Python 如何从不同的文件夹导入模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49039436/
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
How to import a module from a different folder?
提问by Gasp0de
I have a project which I want to structure like this:
我有一个项目,我想这样构建:
myproject
__init__.py
api
__init__.py
api.py
backend
__init__.py
backend.py
models
__init__.py
some_model.py
Now, I want to import the module some_model.py
in both api.py
and backend.py
. How do I properly do this?
现在,我想some_model.py
在api.py
和 中导入模块backend.py
。我该如何正确地做到这一点?
I tried:
我试过:
from models import some_model
but that fails with ModuleNotFoundError: No module named 'models'
.
但这失败了ModuleNotFoundError: No module named 'models'
。
I also tried:
我也试过:
from ..models import some_model
which gave me ValueError: attempted relative import beyond top-level package
.
这给了我ValueError: attempted relative import beyond top-level package
。
What am I doing wrong here? How can I import a file from a different directory, which is not a subdirectory?
我在这里做错了什么?如何从不是子目录的不同目录导入文件?
采纳答案by wim
Firstly, this import statement:
首先,这个导入语句:
from models import some_model
should be namespaced:
应该命名空间:
# in myproject/backend/backend.py or myproject/api/api.py
from myproject.models import some_model
Then you will need to get the directory which contains myproject
, let's call this /path/to/parent
, into the sys.path
list. You can do this temporarily by setting an environment variable:
然后,您需要将包含 的目录myproject
(我们称之为/path/to/parent
)放入sys.path
列表中。您可以通过设置环境变量来临时执行此操作:
export PYTHONPATH=/path/to/parent
Or, preferably, you can do it by writing a setup.py
file and installing your package. Follow the PyPApackaging guide. After you have written your setup.py
file, from within the same directory, execute this to setup the correct entries in sys.path
:
或者,您最好可以通过编写setup.py
文件并安装您的软件包来完成。遵循PyPA打包指南。编写setup.py
文件后,从同一目录中,执行此操作以在 中设置正确的条目sys.path
:
pip install --editable .
回答by Jordan Mattiuzzo
Unfortunately, Python will only find your file if your file is in the systems path. But fear not! There is a way around this!
不幸的是,如果您的文件在系统路径中,Python 只会找到您的文件。但不要害怕!有办法解决这个问题!
Using python's sys
module, we can add a directory to the path just while Python is running, and once Python stops running, it will remove it from the path.
使用python的sys
模块,我们可以在Python运行的时候在路径中添加一个目录,一旦Python停止运行,它就会从路径中删除它。
You can do this by:
您可以通过以下方式执行此操作:
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import [file]
It is important to import sys and set the directory path before you import the file however.
但是,在导入文件之前导入 sys 并设置目录路径很重要。
Good luck!
祝你好运!
Jordan.
约旦。
回答by Gasp0de
Although the above answers are all kind of correct, I found one more answer, which I want to add for completeness. Apparently, the directory in which the originally executed script resides and it's subdirectories are automativally added to the path list.
虽然上述答案都是正确的,但我又找到了一个答案,为了完整起见,我想补充一下。显然,最初执行的脚本所在的目录及其子目录会自动添加到路径列表中。
This means that if you create a start script(e.g. main.py) to your projects root, which will later execute all the other modules (api.py and backend.py in my case) then the above method using
这意味着,如果您创建一个启动脚本(例如 main.py)到您的项目根目录,该脚本稍后将执行所有其他模块(在我的情况下为 api.py 和 backend.py)然后使用上述方法
from models import some_model
will work.
将工作。
回答by Allen Windhorn
This was answered here: Importing files from different folder, in a number of complicated ways. You can also use:
这是在此处回答的:以多种复杂的方式从不同文件夹导入文件。您还可以使用:
import os
导入操作系统
os.chdir('Your/New/Directory")
os.chdir('你的/新的/目录")
import whatever
进口任何东西
Which is very inelegant but does the job. Remember to change back if you need more from the previous directory.
这是非常不雅的,但可以完成工作。如果您需要上一目录中的更多内容,请记住改回来。
Allen
艾伦