Error attempting to import local files as packages
Given a directory structure that looks like:
top/
├── script.py
├── requirements.txt
└── pylib_local
├── __init__.py
└── module.py
And script.py contains a line from pylib_local import module
When I run pex -r requirements.txt -- script.py from within top/, I see "ImportError: No module named pylib_local".
Is this not supported, or am I missing some necessary configuration?
+1 on this, i would like to know the answer to this too.
seems like a possible bug/regression to me - . is not on the sys.path fwict.
any idea which version of pex this started at for you?
repro:
[omerta show]$ cat test.py
from test_test_test import tester
tester()
[omerta show]$ cat test_test_test/__init__.py
def tester():
print('success')
[omerta show]$ python2.7 test.py
success
[omerta show]$ pex -- test.py
Traceback (most recent call last):
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 365, in execute
self._wrap_coverage(self._wrap_profiling, self._execute)
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 293, in _wrap_coverage
runner(*args)
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 325, in _wrap_profiling
runner(*args)
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 411, in _execute
return self.execute_interpreter()
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 421, in execute_interpreter
self.execute_content(name, content)
File "/private/var/folders/bb/msvj_4_s741g93nzgrj19twc0000gn/T/tmpeJHvLa/.bootstrap/_pex/pex.py", line 451, in execute_content
exec_function(ast, globals())
File "<exec_function>", line 4, in exec_function
File "test.py", line 1, in <module>
from test_test_test import tester
ImportError: No module named test_test_test
workaround:
[omerta show]$ PYTHONPATH=. pex -- test.py
success
Sorry @kwlzn i've only just started using pex so no idea when this bug was introduced, not sure if this helps but to confirm, i'm running python 2.7.14 64bit on Linux (arch-linux) with latest release of pex (as shown on pypi and pulled down via pip).
Thanks for showing a workaround, i will test that tonight (in around 8 hours) and let you know how i get on with it.
Ok I can confirm the workaround does indeed fix it for me, I see you've made some progress on a possible perm fix and will be happy to give you feedback once it's available.
Sorry for the massive delay. I'm in this general area in the codebase fixing #707 so I'll take a look.
This now works:
:; tree .
.
├── pylib_local
│ ├── __init__.py
│ └── module.py
├── requirements.txt
└── script.py
2 directories, 4 files
:; cat script.py
from cowsay import tux
from pylib_local import module
if __name__ == "__main__":
tux(module.__file__)
:; cat requirements.txt
cowsay
:; pex -r requirements.txt -- script.py
______________________________
| /tmp/top/pylib_local/module.py |
==============================
\
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
:; pex -V
2.16.0