pex icon indicating copy to clipboard operation
pex copied to clipboard

Error attempting to import local files as packages

Open abstiles opened this issue 9 years ago • 5 comments

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?

abstiles avatar Jan 06 '17 20:01 abstiles

+1 on this, i would like to know the answer to this too.

binhex avatar Jan 17 '18 22:01 binhex

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

kwlzn avatar Jan 18 '18 01:01 kwlzn

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.

binhex avatar Jan 18 '18 09:01 binhex

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.

binhex avatar Jan 18 '18 20:01 binhex

Sorry for the massive delay. I'm in this general area in the codebase fixing #707 so I'll take a look.

jsirois avatar Sep 09 '19 01:09 jsirois

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

jsirois avatar Aug 09 '24 20:08 jsirois