I have a Python 3 project I wanted to release. Here is the structure:
README.rst
setup.cfg
setup.py
projectname
├── __init__.py
├── __main__.py
├── file1.py
└── file2.py
In my setup.py I have the usual info (name, description etc.) and two relevant lines are:
packages=find_packages(),
entry_points={
'console_scripts': [
'project-name=projectname.__main__:main',
],
},
In my __main__.py
, I do the following imports:
from file1 import a
from file2 import b
My project works fine when running __main__.py
in the terminal normally:
python3 __main__.py
However, after running setup.py:
sudo python3 setup.py install
and running:
project-name
I get the following error:
ImportError: No module named 'file1'
What is strange is that if I do explicit relative imports instead:
from .file1 import a
from .file2 import b
This will work for the installed package but NOT when running __main__.py
normally in the terminal where I will get the following error:
SystemError: Parent module '' not loaded, cannot perform relative import
I am completely lost here, can anybody shed some light? I have been reading on imports in Python 3 and nothing has helped me yet. Any help would be much appreciated. Please let me know if addition information is required. Thanks!