سؤال

This questions is detailing a behavior that I can't explain to myself.

src/package/__init__.py is empty but present.

src/package/subpackage/__init__.py:

pink = 'It works'

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. Please note that import package doesn't work either.

NB: (Running an interpreter from src and typing the import statement works perfectly well.

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. If you what to call test.py, you can move your test.py file to src/ directory, or add src to $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. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path

>>> import sys
>>> sys.path

The output is like this

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

This means that the current directory is in sys.path as well. 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.

نصائح أخرى

On python package/test/test.py fails, it's also ran from src:

  1. when you starts a intepreter from src, '' is in sys.path, so path of src could be found;
  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. That's saying, python adds path of the file to sys.path, not the path where you run the script.

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. If the script directory is not available (e.g. 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. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top