0

Python module with an entry point (same as here). The __main__ file imports another:

from myfile import MyClass

myfile.py is a file in the same folder, mymodule. It contains the class called MyClass. This works under regular Python, if I run python3 __main__.py.

Now, I've installed mymodule as a module with sudo pip3 install . on MacOS X (EDIT: same error on Linux). When I run mymodule from the command line, I get a ModuleNotFoundError for myfile:

Traceback (most recent call last):
File "/Users/valekseyev/.pyenv/versions/3.7.3/bin/mymodule", line 11, in <module>
load_entry_point('mymodule==0.50', 'gui_scripts', 'mymodule')()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2793, in load_entry_point
return ep.load()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2411, in load
return self.resolve()
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2417, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/mymodule/__main__.py", line 5, in <module>
from myfile import MyClass
ModuleNotFoundError: No module named 'myfile'

The file myfile.py is present alongside __main__.py under /Users/valekseyev/.pyenv/versions/3.7.3/lib/python3.7/site-packages/mymodule/, I've checked. There's an __init__.py in the folder, too, it's blank. The folder is listed under packages in setup.py.

What's going on please?

EDIT: looking at Python modules out there, they import other files from the same folder with the following syntax:

from .myfile import MyClass

If I try that, the regular Python runs break:

ImportError: attempted relative import with no known parent package

Any way around that, maybe?

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281

1 Answers1

0

Found one way around. I've prepended the file names with . in the import lines, created a file one level up (on the same level as setup.py) that would treat the mymodule folder as a package:

from mymodule.__main__ import main
main()

and I can run that. There's yet another issue I'm facing (library modules take precedence over patched versions), but not this one :)

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281