简体   繁体   English

Python路径说明:从子包导入

[英]Python path explained: import from a subpackage

This questions is detailing a behavior that I can't explain to myself. 这个问题详细说明了我无法向自己解释的行为。

src/package/__init__.py is empty but present. src/package/__init__.py为空但存在。

src/package/subpackage/__init__.py : src/package/subpackage/__init__.py

pink = 'It works'

src/package/test/test.py : src/package/test/test.py

import package.subpackage as subpackage
# I also tried `import package.subpackage as subpackage

print subpackage.pink

Calling from src : python package/test/test.py just fails with ImportError: No module named subpackage . src调用: python package/test/test.py只是失败,并出现ImportError: No module named subpackage Please note that import package doesn't work either. 请注意, import package也不起作用。

NB: (Running an interpreter from src and typing the import statement works perfectly well. 注意:(从src运行解释器并键入import语句效果很好。

Should I understand that I'm not suppose to call subfile of a package? 我是否应该理解我不应该调用包的子文件? In my project it's a test file so it sounds logical for me have it here. 在我的项目中,这是一个测试文件,因此在这里存放对我来说听起来很合逻辑。

Why the current working directory is not in the import path? 为什么当前工作目录不在导入路径中?

Many thanks for those who reads and those who answers. 非常感谢那些阅读和回答的人。

Because you package is not in $PYTHONPATH. 因为您的软件包不在$ PYTHONPATH中。 If you what to call test.py, you can move your test.py file to src/ directory, or add src to $PYTHONPATH 如果您要调用test.py,则可以将test.py文件移动到src /目录,或将src添加到$ PYTHONPATH

PYTHONPATH="/path/to/src:$PYTHONPATH"
export PYTHONPATH

From Documentation 从文档

When a module named spam is imported, the interpreter first searches for a built-in module with that name. 导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。 If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path 如果找不到,它将在变量sys.path给出的目录列表中搜索名为spam.py的文件。

>>> import sys
>>> sys.path

The output is like this 输出是这样的

['.', '/usr/bin', ...

This means that the current directory is in sys.path as well. 这意味着当前目录也位于sys.path中。 If you want to import a module, please make sure that the module path is in sys.path, by adding your package directory to the environment variable PYTHONPATH, or changing your current directory or script directory to the package directory. 如果要导入模块,请通过将软件包目录添加到环境变量PYTHONPATH或将当前目录或脚本目录更改为软件包目录来确保模块路径在sys.path中。

On python package/test/test.py fails, it's also ran from src : python package / test / test.py失败的情况下,它也从src运行

  1. when you starts a intepreter from src , '' is in sys.path , so path of src could be found; 当您从src启动解释器时, ''位于sys.path ,因此可以找到src路径;
  2. when you run python package/test/test.py from src , '' is missing from sys.path , although os.path.abspath('.') shows current dir is "<xxx>\\\\src" , "<xxx>\\\\src" is not in sys.path , while "<xxx>\\\\src\\\\package\\\\test" is in sys.path . 当运行python package/test/test.pysrc''是从缺失sys.path ,虽然os.path.abspath('.')示出了当前目录是"<xxx>\\\\src" "<xxx>\\\\src"不在sys.path ,而"<xxx>\\\\src\\\\package\\\\test"sys.path That's saying, python adds path of the file to sys.path , not the path where you run the script. 就是说,python将文件的路径添加到sys.path ,而不是您运行脚本的路径。

see what the docs says : 看看文档怎么说

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. 在程序启动时初始化后,该列表的第一项path [0]是包含用于调用Python解释器的脚本的目录。 If the script directory is not available (eg if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. 如果脚本目录不可用(例如,如果交互式调用解释器或从标准输入中读取脚本),则path [0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。 Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH. 请注意,由于PYTHONPATH的结果,在插入条目之前插入了脚本目录。

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

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