Background:
I have a directory structure like so:
Package/
setup.py
src/
__init__.py
__main__.py
code.py
I want to be able to run the code in a lot of different ways.
pip install Packageand thenpythonand thenfrom Package import *python -m Packagewhich should do the thing in__main__.pypython __main__.pywhich should also do the thing in__main__.pybut this time, we assume you've downloaded source rather thanpip installing.
Now I've gotten the first two to work, but with a messy setup:
setup.py:
setup(
name='Package',
packages=['Package'],
package_dir={'Package': 'src'},
...
entry_points={ 'console_scripts': ['Package = src.__main__:main' ] }
__init__.py:
from Package.code import .......
__main__.py:
from . import .......
What would make more sense to me would be in both cases to write
from code import ........
but that gives me import errors.
Question:
Is the way I have it really the only way?
And most importantly, how do I support the third use case? Right now, python __main__.py throws
File "__main__.py", line 10, in <module>
from . import code
ImportError: cannot import name 'class defined in code.py'
Notes:
I have read
- https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
- http://setuptools.readthedocs.io/en/latest/setuptools.html
- The many questions here that look like this one but don't answer my question above.