问题:无法在Python中导入我自己的模块

我很难理解模块导入在Python中是如何工作的(我以前从未用任何其他语言来完成过此工作)。

假设我有:

myapp/__init__.py
myapp/myapp/myapp.py
myapp/myapp/SomeObject.py
myapp/tests/TestCase.py

现在,我试图得到这样的东西:

myapp.py
===================
from myapp import SomeObject
# stuff ...

TestCase.py
===================
from myapp import SomeObject
# some tests on SomeObject

但是,我肯定做错了,因为Python看不到这myapp是一个模块:

ImportError: No module named myapp

I’m having a hard time understanding how module importing works in Python (I’ve never done it in any other language before either).

Let’s say I have:

myapp/__init__.py
myapp/myapp/myapp.py
myapp/myapp/SomeObject.py
myapp/tests/TestCase.py

Now I’m trying to get something like this:

myapp.py
===================
from myapp import SomeObject
# stuff ...

TestCase.py
===================
from myapp import SomeObject
# some tests on SomeObject

However, I’m definitely doing something wrong as Python can’t see that myapp is a module:

ImportError: No module named myapp

回答 0

在您的特定情况下,您似乎正在尝试SomeObject从myapp.py和TestCase.py脚本导入。在myapp.py中,执行

import SomeObject

因为它在同一个文件夹中。对于TestCase.py,请执行

from ..myapp import SomeObject

但是,仅当您从软件包中导入TestCase时,此方法才有效。如果要直接运行python TestCase.py,则必须弄乱路径。这可以在Python中完成:

import sys
sys.path.append("..")
from myapp import SomeObject

尽管通常不建议这样做。

通常,如果您希望其他人使用您的Python软件包,则应使用distutils创建安装脚本。这样,任何人都可以使用像这样的命令轻松安装您的软件包,python setup.py install并且该软件包将在其计算机上的所有位置可用。如果您对软件包很认真,甚至可以将其添加到Python软件包索引PyPI中

In your particular case it looks like you’re trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do

import SomeObject

since it is in the same folder. For TestCase.py, do

from ..myapp import SomeObject

However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:

import sys
sys.path.append("..")
from myapp import SomeObject

though that is generally not recommended.

In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you’re serious about the package, you could even add it to the Python Package Index, PyPI.


回答 1

该函数import在PYTHONPATH env中查找文件。变量和您的本地目录。因此,您可以将所有文件放在同一目录中,也可以将键入的路径导出到终端中:

export PYTHONPATH="$PYTHONPATH:/path_to_myapp/myapp/myapp/"

The function import looks for files into your PYTHONPATH env. variable and your local directory. So you can either put all your files in the same directory, or export the path typing into a terminal::

export PYTHONPATH="$PYTHONPATH:/path_to_myapp/myapp/myapp/"

回答 2

导出路径是一个好方法。另一种方法是将.pth添加到您的站点包位置。在我的Mac上,我的python将站点包保存在/ Library / Python中,如下所示

/Library/Python/2.7/site-packages

我在/Library/Python/2.7/site-packages/awesome.pth创建了一个名为awesome.pth的文件,并在文件中放置了以下引用我的超赞模块的路径

/opt/awesome/custom_python_modules

exporting path is a good way. Another way is to add a .pth to your site-packages location. On my mac my python keeps site-packages in /Library/Python shown below

/Library/Python/2.7/site-packages

I created a file called awesome.pth at /Library/Python/2.7/site-packages/awesome.pth and in the file put the following path that references my awesome modules

/opt/awesome/custom_python_modules

回答 3

你可以试试

from myapp.myapp import SomeObject

因为您的项目名称与myapp.py相同,因此它会首先搜索项目文档

You can try

from myapp.myapp import SomeObject

because your project name is the same as the myapp.py which makes it search the project document first


回答 4

在您的第一个myapp目录中,u可以添加setup.py文件,并在setup.py中添加两个python代码

from setuptools import setup
setup(name='myapp')

在命令行的第一个myapp目录中,使用pip install -e。安装软件包

In your first myapp directory ,u can add a setup.py file and add two python code in setup.py

from setuptools import setup
setup(name='myapp')

in your first myapp directory in commandline , use pip install -e . to install the package


回答 5

pip installWindows 10上的默认设置为安装在“ Program Files / PythonXX / Lib / site-packages”中,该目录需要管理权限。因此,我通过以管理员身份运行pip install解决了我的问题 (即使您使用管理员帐户登录,也必须以管理员身份打开命令提示符)。另外,从python调用pip更安全。
例如
python -m pip install <package-name>
代替
pip install <package-name>

pip install on Windows 10 defaults to installing in ‘Program Files/PythonXX/Lib/site-packages’ which is a directory that requires administrative privileges. So I fixed my issue by running pip install as Administrator (you have to open command prompt as administrator even if you are logged in with an admin account). Also, it is safer to call pip from python.
e.g.
python -m pip install <package-name>
instead of
pip install <package-name>


回答 6

就我而言,尽管Windows文件名不区分大小写,但Python导入却使Windows vs Python感到惊讶。因此,如果您有Stuff.py文件,则需要按原样导入此名称。

In my case it was Windows vs Python surprise, despite Windows filenames are not case sensitive, Python import is. So if you have Stuff.py file you need to import this name as-is.


回答 7

你需要

__init__.py

在所有您需要与之交互的代码的文件夹中。即使您尝试导入的文件处于同一级别,也需要在每次导入时指定项目的顶级文件夹名称。

You need to have

__init__.py

in all the folders that have code you need to interact with. You also need to specify the top folder name of your project in every import even if the file you tried to import is at the same level.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。