I’ve built a package which I distributed through PyPI.
The upload, download, installation, and the package itself, all work fine. My problem is that in order to run it I need to call the package name twice, which I want to avoid.
I want it to go like this:
import package_name
package_name.do_stuff()
But it only works like this:
from package_name import module_name
module_name.do_stuff()
Rather than having package_name
be the same as module_name
, or have two different names, I just one the one. I suppose its because of the way the project folder is structured, like so:
package_name/
|
- setup.py
- requirements.txt
- package_name/
|
- __init.py__
- module_name.py
- dist
- README etc
I guess what I’m asking is how can I structure my project folder so that I get neat usage in the form of:
pip install package_name
import package_name
package_name.do_stuff()
Thanks in advance
Advertisement
Answer
This can be performed by importing module_name
in package_name/package_name/__init__.py
.
Given your exact structure, with:
# package_name/package_name/module_name.py
class Foo:
pass
# package_name/package_name/__init__.py
from package_name.module_name import Foo
Then you should be able to perform
from package_name import Foo
from e.g. package_name/test.py
.