Say I've got a repository with the following structure:
-sample_package
-sample_package
-__init__.py
-main_file.py
-utilities_to_import
-__init__.py
-utilities.py
-setup.py
-README.md
in main_file.py, I'm able to import the utilities sub-package via the command below. And when I run the main_file.py code, all is good.
from utilities_to_import import utilities
BUT, when exporting sample_package via pip install -e ., I get a module not found issue stating that the utilities_to_import module is not found.
My setup.py looking like this:
from setuptools import setup
setup(name='sample_package_ec',
version='0.1',
description='The funniest joke in the world',
url='http://github.com/storborg/funniest',
author='Flying Circus',
author_email='flyingcircus@example.com',
license='MIT',
packages=['sample_package_ec','sample_package_ec.utilities_to_import'],
zip_safe=False)
Once I export the package via pip install -e ., then import it via "from sample_package_ec import main_file", I'm getting:
ModuleNotFoundError: No module named 'utilities_to_import'
How come the utilities_to_import file is not found when I have no problem running main_file.py locally, which imports utilities_to_import? I've tried to modify the setup.py and init.py to include the utilities_to_import package with no luck.
Note: I also "fixed" the issue by adding the path to the package to path after importing sample_package, but obviously that's not a nice way to export a package. How can I essentially add utilities_to_import to path by default into the sample_package?
EDIT: solved via changing the main_file.py import statements as the following (below). My concern is that I'm not sure this is the best way to go about it? aka I thought relative imports were bad.
if __package__ is None or __package__ == '':
# uses current directory visibility
from utilities_to_import import utilities
print('not a package')
else:
# uses current package visibility
print('yay package')
from .utilities_to_import import utilities