I have the following project structure:
project
|-project.py
|-__init__.py
|-setup.py
|-lib|-__init__.py|-project|-__init__.py|-tools.py
with project.py
:
from project.lib import *def main():print("main")tool()if __name__ == "__main__":main()
setup.py
:
from setuptools import setupsetup(name = "project",version="1.0",packages = ["project", "project.lib"],package_dir = {"project": ".", "project.lib": 'lib/project'},entry_points={'console_scripts': ['project = project.project:main',],},
)
tools.py
:
def tool():print("tool")
If I run
import project.lib.tools
project.lib.tools.tool()
it works as expected, but running the command project
fails with
Traceback (most recent call last):File "/usr/local/bin/project", line 9, in <module>load_entry_point('project==1.0', 'console_scripts', 'project')()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 568, in load_entry_pointreturn get_distribution(dist).load_entry_point(group, name)File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2720, in load_entry_pointreturn ep.load()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2380, in loadreturn self.resolve()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2386, in resolvemodule = __import__(self.module_name, fromlist=['__name__'], level=0)File "build/bdist.linux-x86_64/egg/project/project.py", line 3, in <module>
ImportError: No module named lib
I don't understand why the two interpreters don't have the same default import pathes.
The reason for this setup is that I want to be able to import project.lib.tools
, but keep the directory structure with lib/project
.
The complete distutils
documentation seriously doesn't say a word on how one can import packages after they have been distributed (the difference of setuptools
and distutils
isn't less misterious - no way of knowing whether the behavior of distutils
is extended here or not).
I'm using setuptools
18.4-1 with python
2.7 on Ubuntu 15.10.
If I change the project structure and setup.py
as suggested in @AnttiHaapala's answer I'm getting
$ project
Traceback (most recent call last):File "/usr/local/bin/project", line 9, in <module>load_entry_point('project==1.0', 'console_scripts', 'project')()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 568, in load_entry_pointreturn get_distribution(dist).load_entry_point(group, name)File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2720, in load_entry_pointreturn ep.load()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2380, in loadreturn self.resolve()File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2386, in resolvemodule = __import__(self.module_name, fromlist=['__name__'], level=0)File "build/bdist.linux-x86_64/egg/project/project.py", line 3, in <module>
ImportError: No module named lib