Python 导入我自己的模块时出现“ImportError: No module named...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35555843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:35:48  来源:igfitidea点击:

"ImportError: No module named..." when importing my own module

pythonpython-2.7python-importpythonanywhere

提问by Lucas Amos

I am trying to import a module and I keep getting an ImportError.

我正在尝试导入一个模块,但我不断收到 ImportError。

In the PortfolioStatus.py file I have the following code which imports the share_data class from the share_data.py module
from Shares.share_data import share_data

在 PortfolioStatus.py 文件中,我有以下代码从 share_data.py 模块导入 share_data 类
from Shares.share_data import share_data

I am getting the following error:

我收到以下错误:

File "/home/lucasamos/FYP/Shares/Communication/PortfolioStatus.py", line 3, in <module>
from Shares.share_data import share_data
ImportError: No module named Shares.share_data

To make things more confusing this works fine on my local machine but I am hosting on PythonAnywhere and this is where I am getting the error

为了让事情更加混乱,这在我的本地机器上运行良好,但我托管在 PythonAnywhere 上,这就是我收到错误的地方

My file hierarchy is show in the image below

我的文件层次结构如下图所示

File hierarchy

文件层次结构

Thanks in advance!

提前致谢!

采纳答案by Lucas Amos

OK so I finally worked it out. As indicated by a few of the answers I needed to add my root folder to the system path.

好的,所以我终于解决了。正如一些答案所示,我需要将我的根文件夹添加到系统路径中。

In the end this is what I did:

最后这就是我所做的:

import sys
sys.path.append("/home/lucasamos/FYP")

回答by CoMartel

you should try this:

你应该试试这个:

import sys
sys.path.append("../Shares/templates")
import share_data

It adds your templates folder to the list of path python is checking for modules.

它将您的模板文件夹添加到 python 正在检查模块的路径列表中。

回答by Jeff

This is likely because your Shares directory is not in your PYTHONPATH.

这可能是因为您的 Shares 目录不在您的 PYTHONPATH 中。

See this article about using PYTHONPATH: https://users-cs.au.dk/chili/PBI/pythonpath.html

请参阅有关使用 PYTHONPATH 的这篇文章:https: //users-cs.au.dk/chili/PBI/pythonpath.html

Excerpt:

摘抄:

Often however, you will need to import a module not located in the same directory as the main program. Continuing the example above, assume you're writing a program located in ~/PBI/ which needs to include mymodule.py.

In order for the Python interpreter to find your module, you need to tell it where to look. You can do that by setting the environment variable PYTHONPATH. Depending on the shell program you use (e.g., xterm), this is done in one of two ways.

Bash:

export PYTHONPATH=${PYTHONPATH}:/users/[your username]/PBI/Modules/

但是,您通常需要导入与主程序不在同一目录中的模块。继续上面的例子,假设你正在编写一个位于 ~/PBI/ 的程序,它需要包含 mymodule.py。

为了让 Python 解释器找到你的模块,你需要告诉它去哪里找。您可以通过设置环境变量 PYTHONPATH 来做到这一点。根据您使用的 shell 程序(例如 xterm),这可以通过以下两种方式之一完成。

重击:

export PYTHONPATH=${PYTHONPATH}:/users/[您的用户名]/PBI/Modules/

回答by Andriy Ivaneyko

Add empty __init__.pyon one level with manage.pyfile.

__init__.pymanage.py文件的一层添加空。

Such inclusion of __init__.pyfile indicates to the Python interpreter that the directory should be treated as a Python package.

__init__.py文件的这种包含向 Python 解释器表明该目录应被视为 Python 包。