All files are empty except foo/script.py, which simply contains import bar.thing.
The directory containing foo is added to the PYTHONPATH environment variable globally.
$ env PYTHONPATH=. python foo/script.py
Traceback (most recent call last):
File "/foo/script.py", line 1, in <module>
import bar.thing
ModuleNotFoundError: No module named 'bar.thing'; 'bar' is not a package
This is expected, since foo/bar.py shadows bar/thing.py due to python adding the directory of the invoked file to sys.path.
If you configure pycharm to run your script with PYTHONSAFEPATH=1 pydevd.py will still unconditionally prepend the scripts directory and you will get the import failure when debugging.
Consider the following project layout:
All files are empty except
foo/script.py
, which simply containsimport bar.thing
. The directory containingfoo
is added to thePYTHONPATH
environment variable globally.This is expected, since
foo/bar.py
shadowsbar/thing.py
due to python adding the directory of the invoked file tosys.path
.Python 3.11 adds
PYTHONSAFEPATH
and-P
options to address this issue.However, when running under
pydevd.py
, the same is not true because of the following lines: https://github.com/fabioz/PyDev.Debugger/blob/pydev_debugger_3_0_3/pydevd.py#L2584-L2589If you configure pycharm to run your script with PYTHONSAFEPATH=1
pydevd.py
will still unconditionally prepend the scripts directory and you will get the import failure when debugging.