How to import your package/modules from a script in bin folder in python
Question:
When organising python project, this structure seems to be a standard way of doing it:
myproject
bin
myscript
mypackage
__init__.py
core.py
tests
__init__.py
mypackage_tests.py
setup.py
My question is, how do I import my core.py
so I can use it in myscript
?
both __init__.py
files are empty.
Content of myscript
:
#!/usr/bin/env python
from mypackage import core
if __name__ == '__main__':
core.main()
Content of core.py
def main():
print 'hello'
When I run myscript
from inside myproject
directory, I get the following error:
Traceback (most recent call last):
File "bin/myscript", line 2, in <module>
from mypackage import core
ImportError: No module named mypackage
What am I missing?
Answers:
Usually, setup.py
should install the package in a place where the Python interpreter can find it, so after installation import mypackage
will work. To facilitate running the scripts in bin
right from the development tree, I’d usually simply add a simlink to ../mypackage/
to the bin
directory. Of course, this requires a filesystem supporting symlinks…
I’m not sure if there is a “best choice”, but the following is my normal practice:
-
Put whatever script I wanna run in /bin
-
do “python -m bin.script” in the dir myproject
-
When importing in script.py, consider the dir in which script.py is sitting as root. So
from ..mypackage import core
If the system supports symlink, it’s a better choice.
I usually add my bin path into $PYTHONPATH, that will enable python to look for asked module in bin directory too.
export PYTHONPATH=/home/username/bin:$PYTHONPATH
$ python
import module_from_bin
I solved the issue following setuptools specifications.
In setup.py you can specify the modules as an argument for the function setup()
:
packages = find_packages()
This finds all modules.
p.s. you have to import this function: from setuptools import setup, find_packages
When organising python project, this structure seems to be a standard way of doing it:
myproject
bin
myscript
mypackage
__init__.py
core.py
tests
__init__.py
mypackage_tests.py
setup.py
My question is, how do I import my core.py
so I can use it in myscript
?
both __init__.py
files are empty.
Content of myscript
:
#!/usr/bin/env python
from mypackage import core
if __name__ == '__main__':
core.main()
Content of core.py
def main():
print 'hello'
When I run myscript
from inside myproject
directory, I get the following error:
Traceback (most recent call last):
File "bin/myscript", line 2, in <module>
from mypackage import core
ImportError: No module named mypackage
What am I missing?
Usually, setup.py
should install the package in a place where the Python interpreter can find it, so after installation import mypackage
will work. To facilitate running the scripts in bin
right from the development tree, I’d usually simply add a simlink to ../mypackage/
to the bin
directory. Of course, this requires a filesystem supporting symlinks…
I’m not sure if there is a “best choice”, but the following is my normal practice:
-
Put whatever script I wanna run in /bin
-
do “python -m bin.script” in the dir myproject
-
When importing in script.py, consider the dir in which script.py is sitting as root. So
from ..mypackage import core
If the system supports symlink, it’s a better choice.
I usually add my bin path into $PYTHONPATH, that will enable python to look for asked module in bin directory too.
export PYTHONPATH=/home/username/bin:$PYTHONPATH
$ python
import module_from_bin
I solved the issue following setuptools specifications.
In setup.py you can specify the modules as an argument for the function setup()
:
packages = find_packages()
This finds all modules.
p.s. you have to import this function: from setuptools import setup, find_packages