无法在 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
在您的特定情况下,您似乎正在尝试从 myapp.py 和 TestCase.py 脚本导入
SomeObject
。从 myapp.py 中执行,因为它位于同一文件夹中。对于 TestCase.py,请执行
但是,只有当您从包中导入 TestCase 时,这才有效。如果你想直接运行 python TestCase.py,你就必须弄乱你的路径。这可以在 Python 中完成:
尽管通常不推荐这样做。
一般来说,如果你想让其他人使用你的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, dosince it is in the same folder. For TestCase.py, do
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: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.函数
import
在 PYTHONPATH 环境中查找文件。变量和您的本地目录。因此,您可以将所有文件放在同一目录中,或者导出在终端中输入的路径: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::你可以尝试一下
,因为你的项目名称与 myapp.py 相同,这使得它首先搜索项目文档
You can try
because your project name is the same as the myapp.py which makes it search the project document first
导出路径是一个好方法。另一种方法是将 .pth 添加到您的站点包位置。
在我的Mac上,我的Python将站点包保留在/Library/Python中,如下所示,
我在/Library/Python/2.7/site-packages/awesome.pth创建了一个名为awesome.pth的文件,并在该文件中放置了引用我的以下路径很棒的模块
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
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
您需要
在所有包含需要与之交互的代码的文件夹中包含该代码。
即使您尝试导入的文件位于同一级别,您还需要在每次导入时指定项目的顶级文件夹名称。
You need to have
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.
在您的第一个 myapp 目录中,您可以添加一个 setup.py 文件,并
在命令行中的第一个 myapp 目录中的 setup.py 中添加两个 python 代码,使用 pip install -e 。安装软件包
In your first myapp directory ,u can add a setup.py file and add two python code in setup.py
in your first myapp directory in commandline , use pip install -e . to install the package
Windows 10 上的
pip install
默认安装在“Program Files/PythonXX/Lib/site-packages”中,该目录需要管理权限。因此,我通过以管理员身份运行 pip install 解决了我的问题(即使您使用管理员帐户登录,也必须以管理员身份打开命令提示符)。另外,从 python 调用 pip 更安全。例如
python -m pip install
而不是
pip install <包名称>
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>
假设我写了一个模块,
我们必须告诉 python 在哪里寻找该模块。我们必须将我们的路径添加到
sys.path
现在
importlib.util.find_spec('my_module')
返回:我们创建了我们的模块,我们通知了 python 它的路径,现在我们应该能够导入它
let's say i write a module
we have to tell python where to look for the module. we have to add our path to the
sys.path
now
importlib.util.find_spec('my_module')
returns:we created our module, we informed python its path, now we should be able to import it
简短的回答:
通过模块标志执行所需的文件对我有用。假设我们有一个典型的目录结构,如下所示:
现在,如果您想在目录中运行一个文件,该文件具有从其他模块导入的内容,您需要做的就是如下所示:
PS:您必须使用点表示法来引用子模块(您要执行的文件/脚本)。我也使用了python3.9+。所以我既不需要任何 init.py 也不需要任何 sys 路径附加语句。
希望有帮助!快乐编码!
Short Answer:
Executing the required file via module flag worked for me. Lets say we got a typical directory structure as below:
Now if you want to run a file inside a directory, that has imports from other modules, all you need to do is like below:
PS: You gotta use dot notation to refer the submodules(Files/scripts you want to execute). Also I used python3.9+. So I didnt require neither any init.py nor any sys path append statements.
Hope that helps! Happy Coding!
就我而言,这是 Windows 与 Python 的惊喜,尽管 Windows 文件名不区分大小写,但 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.这对我有用:
.
表示它将从父模块中搜索任何本地模块。This worked for me:
The
.
signifies that it will search any local modules from the parent module.如果您使用 IPython 控制台,请确保您的 IDE(例如,spyder)指向正确的工作目录(即您的项目文件夹)
If you are using the IPython Console, make sure your IDE (e.g., spyder) is pointing to the right working directory (i.e., your project folder)
除了建议的解决方案(如已接受的答案)之外,我在 Pycharm 中也遇到了同样的问题,并且我不想像上面建议的相对寻址那样修改导入。
我终于发现,如果我在解释器设置中将我的src/(Python代码的根目录)标记为源,问题就会得到解决。
Besides the suggested solutions like the accepted answer, I had the same problem in Pycharm, and I didn't want to modify imports like the relative addressing suggested above.
I finally found out that if I mark my src/ (root directory of my python codes) as the source in Interpreter settings, the issue will be resolved.
我将自定义模块路径添加到 python3*._pth 文件中。它将位于 python 安装目录中。
这解决了问题
I added my custom modules path to python3*._pth file.It will be located in the python installed directory.
This resolved the issue
如果您使用 Anaconda,您可以执行以下操作:
从 Shell 中,它会将您的路径写入 conda.pth 文件,并将其写入第 3 方模块的标准目录(在我的例子中为站点包)。
If you use Anaconda you can do:
from the Shell and it will write your path into a conda.pth file into the standard directory for 3rd party modules (site-packages in my case).