I am transitioning to a package-based workflow for a project I've been working on. I want to be able to separate development and production environments and I think setuptools offers this possibility with some degree of ease.
I have a project structured as follows:
modulename/
setup.py
modulename/
file_a.py
script.py
In script.py, I want to import file_a.py. Currently I do this by doing import file_a
.
My setup.py looks like:
from setuptools import setup, find_packages
setup(name='modulename',
packages = find_packages(),
package_dir = {'': '../modulename'},
scripts = ['modulename/script.py'])
Currently, when I run script.py
after doing python setup.py install
, I get an error message:
SystemError: Parent module '' not loaded, cannot perform relative import
I have tried a variety of permutations of package_dir = ...
, most notably package_dir = {'': 'modulename'}
, but this throws another error on install, error: package directory 'modulename/modulename' does not exist
I am not sure what I am doing wrong. The documentation online for setuptools is relatively poor in dealing with situations involving relative imports. Can someone point me in the right direction?
The problem is not related with setuptools
. Using relative imports inside the module being executed as __main__
does not work out of the box. There are workarounds / hacks, but the most common solutions seems to be moving the script out of the package or using absolute imports in the script file.
Take a look at Relative imports in Python 3 for the full story.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With