Reputation: 2877
I have a Git repository cloned into myproject
, with an __init__.py
at the root of the repository, making the whole thing an importable Python package.
I'm trying to write a setuptools setup.py
for the package, which will also sit in the root of the repository, next to the __init__.py
file. I want setup.py
to install the directory it resides in as a package. It's fine if setup.py itself comes along as part of the installation, but it would be better if it didn't. Ideally this should work also in editable mode (pip install -e .
)
Is this configuration at all supported? I can kind of make it work by having a package_dir= {"": ".."},
argument to setup()
, telling it to look for myproject
in the directory above the current one. However, this requires the package to always be installed from a directory named myproject
, which does not appear to be the case if, say, it's being installed through pip
, or if someone is working out of a Git clone named myproject-dev
, or in any number of other cases.
Another hack I'm contemplating is a symlink to .
named mypackage
inside of the repository. That ought to work, but I wanted to check if there was a better way first.
Upvotes: 15
Views: 6976
Reputation: 22295
See also Create editable package setup.py in the same root folder as __init__.py
As far as I know this should work:
myproject-dev/
├── __init__.py
├── setup.py
└── submodule
└── __init__.py
#!/usr/bin/env python3
import setuptools
setuptools.setup(
name='MyProject',
version='0.0.0.dev0',
packages=['myproject', 'myproject.submodule'],
package_dir={
'myproject': '.',
},
)
One way to make this work for editable or develop installations is to manually modify the easy-install.pth
file.
Assuming:
/home/user/workspace/empty/project
;.venv
is used;python3 -m pip install -e .
or python3 setup.py develop
;Then:
/home/user/workspace/empty/project/.venv/lib/python3.6/site-packages/easy-install.pth
;/home/user/workspace/empty/project
.In order to let the imports work as expected one can edit this line to read the following:
/home/user/workspace/empty
Note:
/home/user/workspace/empty
that looks like a Python package is then susceptible to be imported, that is why it is a good idea to place the project in its own directory, in this case the directory empty
contains nothing else but the directory project
.project.setup
is also importable.Upvotes: 8