简体   繁体   English

from x import y 适用于 Windows 但不适用于 Ubuntu

[英]from x import y works on Windows but not on Ubuntu

I'm trying to import a class from a different file but it keeps giving me a "ModuleNotFoundError: No module named 'x'" error.我正在尝试从不同的文件导入一个类,但它一直给我一个“ModuleNotFoundError: No module named 'x'”错误。

I'm trying to make myself a little Telegram bot that can do a variety of things for me.我正在努力让自己成为一个可以为我做各种事情的小电报机器人。 I am using PyCharm on Windows to code.我在 Windows 上使用 PyCharm 进行编码。 Everything works on Windows.一切都在 Windows 上运行。 But after I copied the code to my VPS it spits out "ModuleNotFoundError".但是在我将代码复制到我的 VPS 后,它会吐出“ModuleNotFoundError”。 I have tried to use relative imports as well as absolute imports but none of them seemed to work.我曾尝试使用相对导入和绝对导入,但它们似乎都不起作用。 Read about relative and absolute imports from here .这里阅读相对和绝对导入。

When using relative imports, I get a different error saying " __main __.fileName " is not a package, which I feel is a step backwards.当使用相对导入时,我得到一个不同的错误,说“__main __.fileName”不是一个包,我觉得这是一个倒退。

I also think that I am having the same issue as the person on this stackexchange post .我也认为我和这个stackexchange post上的人有同样的问题。 I did what the answer there said and added an empty " init .py" file, but I had no changes in output.我按照那里的答案做了,并添加了一个空的“ init .py”文件,但我的输出没有任何变化。 Then I saw that, correct me if I am wrong, Python 3.3 and after does not need an empty init .py file in each of the subdirectories.然后我看到了,如果我错了,请纠正我,Python 3.3 及之后的每个子目录中都不需要空的init .py 文件。 But I still have them.但我仍然拥有它们。 Just in case.以防万一。

I've already gone through a bunch of stackoverflow questions on kind of the same issue.我已经就类似的问题解决了一堆stackoverflow问题。 But none of the answers are really solutions to my issue in my opinion.但在我看来,没有一个答案能真正解决我的问题。

Here is my directory structure right now.这是我现在的目录结构。

baivrau-bot/
├── env.py
├── imgurDownloader
│   ├── __init__.py
│   ├── downloader.py
│   ├── main.py
│   ├── readme.md
│   └── test.py
├── readme.md
├── requirements.txt

Here is the error I am getting.这是我得到的错误。 Line 10 is the culprit.第 10 行是罪魁祸首。

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    from imgurDownloader.downloader import ImgurAlbumDownloader
ModuleNotFoundError: No module named 'imgurDownloader'

Here are lines 1-16 on main.py这是 main.py 上的第 1-16 行

import telepot
from telepot.namedtuple import InputMediaPhoto
import glob
import os
import re
import time
import sys
import shutil
from hashlib import md5
from imgurDownloader.downloader import ImgurAlbumDownloader
from env import bot_token

chat_id = sys.argv[1]
imgur_link = sys.argv[2]

bot = telepot.Bot(bot_token)

The file 'downloader' is from a Github repo .文件“下载器”来自Github 存储库

I used PyCharm on my Windows computer and it works completely fine.我在我的 Windows 计算机上使用了 PyCharm,它运行良好。 I except the same when running on Ubuntu or any linux distro.在 Ubuntu 或任何 linux 发行版上运行时,我除外。

Sounds like you didn't set your PYTHONPATH variable where he should search for your packages.听起来你没有设置你的PYTHONPATH变量,他应该在那里搜索你的包。

For single test try in your command line:对于单个测试,请在命令行中尝试:

export PYTHONPATH="$/pwd/path_to_dir"

before you start your script.在你开始你的脚本之前。 But you should definitely set this variable permanently.但是你绝对应该永久设置这个变量。

You're probably not accessing main.py from the same folder.您可能不是从同一文件夹访问main.py Check your working directory.检查您的工作目录。 What edition of PyCharm are you using?您使用的是哪个版本的 PyCharm? Can you run it from the terminal?你可以从终端运行它吗? Also, did you try removing the directory prefix so it's just from downloader import ImgurAlbumDownloader ?另外,您是否尝试删除目录前缀,以便它只是from downloader import ImgurAlbumDownloader

My final tip would be to follow a conventional project structure where your tests are in a different folder: What is the best project structure for a Python application?我的最后一个建议是遵循传统的项目结构,其中您的测试位于不同的文件夹中: Python 应用程序的最佳项目结构是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM