Setting package_dir to ..?
Asked Answered
C

1

15

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.

Coursing answered 19/7, 2013 at 22:56 Comment(0)
B
8

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:

  • the project lives in: /home/user/workspace/empty/project;
  • a virtual environment .venv is used;
  • the project is installed with python3 -m pip install -e . or python3 setup.py develop;
  • the Python version is 3.6.

Then:

  • the file is found at a location such as /home/user/workspace/empty/project/.venv/lib/python3.6/site-packages/easy-install.pth;
  • its content is: /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:

  • Everything in /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.
  • The module project.setup is also importable.
Bucovina answered 17/10, 2019 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.