简体   繁体   中英

Setting package_dir to ..?

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.

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': '.',
    },
)

Update

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.

使用符号链接效果不佳(您应该获得递归嵌套目录),请参阅我对另一篇文章的回答以了解丑陋的黑客,但它具有使用pip的优势。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM