简体   繁体   English

从Python中的单独路径导入模块

[英]Importing modules from separate paths in Python

I'm trying to import modules from separate paths but the error it's returning is "module not found." 我正在尝试从单独的路径导入模块,但是返回的错误是“找不到模块”。 It's importing modules from the directory the script is executed in but will not change directories and import from said directory. 它从执行脚本的目录中导入模块,但不会更改目录并从该目录中导入。

print(os.getcwd())

When I run this, before it throws the error it couldn't find the module, it will output the parent directory so for example I'll go with test\\import\\modules . 当我运行它时,在抛出错误之前找不到模块,它将输出父目录,因此例如,我将使用test \\ import \\ modules

I'll run a script in \\import\\ to import test_0.pyd from \\import\\ and test_1.pyd from \\modules (test.py and test_0 being located in \\import\\ and test_1 being located in \\modules . Also, I have tried relative importing and every directory contains init .py ). 我将在\\ import \\中运行一个脚本,以从\\ import \\中导入test_0.pyd和从\\ modules中 导入 test_1.pyd(test.py和test_0位于\\ import \\中,而test_1位于\\ modules中 。此外,我还有尝试了相对导入,并且每个目录都包含init .py )。

import test_0 # this would work
from modules import test_1 # throws error that module isn't found

So I run the print command and it returns that it's trying to import from test\\ and I've tried changing directories but it'll say the working directory changed when I print, but still outputs that it couldn't find the module. 因此,我运行了print命令,它返回的是它试图从test \\导入,并且我尝试过更改目录,但是它将说工作目录在我打印时已更改,但仍然输出找不到模块。 Any help is greatly appreciated, thank you. 非常感谢您的任何帮助,谢谢。

EDIT http://prntscr.com/6ch7fq - executing test.py http://prntscr.com/6ch80q - import directory 编辑 http://prntscr.com/6ch7fq-执行test.py http://prntscr.com/6ch80q-导入目录

When you start python from a directory, that directory is added to your PYTHONPATH so modules are importable from that directory and below, provided you've got an __init__.py in each directory, including the top level that you're running python from. 当您从某个目录启动python时,该目录将添加到您的PYTHONPATH因此可以从该目录及其下导入模块,前提是您在每个目录中都有一个__init__.py ,其中包括从其运行python的顶层。 See here: 看这里:

~/Development/imports $ tree . ├── __init__.py ├── mod1 │ ├── __init__.py │ ├── a.py ├── mod2 │ ├── __init__.py │ ├── b.py ├── top.py

So when we start python from ~/Development/imports/ , we can access top mod1.a and mod2.b : 因此,当我们从~/Development/imports/启动python时,我们可以访问top mod1.amod2.b

~/Development/imports $ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import top
>>> import mod1.a
>>> import mod2.b
>>> import sys

But when we start python from inside mod1 , we're not allowed to go outside of mod1 back to top or to mod2 : 但是,当我们从内部开始蟒蛇mod1 ,我们不允许到外面去的mod1topmod2

~/Development/imports/mod1 $ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> from .. import top
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>> from ..mod2 import b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package

Relative imports from ..mod2 import b only work from a module below the top level module that you started from, because they are all implicitly in the python path. from ..mod2 import b相对from ..mod2 import b只能在您从其开始的顶级模块下面的模块中工作,因为它们都隐式位于python路径中。

You can't escape outside of the module you start from unless that particular path is added to PYTHONPATH or sys.path : 你无法逃避你,除非那个特定的路径添加到启动模块之外 PYTHONPATHsys.path

~/Development/imports/mod1 $ PYTHONPATH=../ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import top
>>> import top.mod2.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mod2.b
>>> import sys
>>> sys.path.append('~/Development/imports/mod2/')
>>> from mod2 import b
>>>

So, you need to ensure that all of your directories have an __init__.py file in them. 因此,您需要确保所有目录中都有__init__.py文件。 You also need to ensure that you're starting python from the right location, usually the top directory. 您还需要确保从正确的位置(通常是顶层目录)启动python。 You can't start python half way down the directory structure and expect to get back up to the top, or sideways to another directory/module. 您无法在目录结构的一半以下启动python并期望回到顶部,或侧向另一个目录/模块。

Do you have __init__.py file in the said modules/directory? 在上述模块/目录中是否有__init__.py文件? This is required for python to treat it as a package. 这是python将其视为包所必需的。

Check out What is __init__.py for? 看看__init__.py的作用是什么?

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

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