Question

For example,there are 3 files in the current directory

A/x.py
A/__init__.py
scripts/b.py

And the first line of b.py looks like this:

from A.x import *

Then I tried to run b.py in the current directory like this:

python scripts/b.py

However, that will leads to an error:

ImportError: No module named A.x

I think the reason is : A.x is in the current directory under which the shell is run, not in the directory scripts where scripts/b.py locates.

Does anyone have ideas about how to solve this problem to enable the python to import from the current Path of shell apart from the path of the script being run?

Was it helpful?

Solution

The module search logic is explained in this section of the docs.

Basically, you need to get the search directory into your sys.path variable. One way of doing this is by setting it in an environment variable PYTHONPATH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top