Setting package_dir to ..?

I have a Git repository cloned into myproject with __init__.py in the root of the repository, which makes all this an importable Python package.

I am trying to write setuptools setup.py for a package that will also be in the root of the repository next to the __init__.py file. I want setup.py set the directory it is located in as a package. It would be nice if setup.py itself was part of the installation, but it would be better if it didn't. Ideally, this should work in editable mode ( pip install -e . )

Is this configuration generally supported? I can make it work by specifying the argument package_dir= {"": ".."}, for setup() and asking it to look for myproject in the directory above the current one. However, this requires that the package is always installed from a directory named myproject , which apparently does not happen if, say, it is installed through pip or someone works with a Git clone named myproject-dev , or in any another number of cases.

Another hack I am considering is a symbolic link to . named mypackage inside the repository. This should work, but I wanted to check if there is a better way in the first place.

+7
source share
2 answers

See also Creating an editable setup.py package 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 do this for editable or under development installations is to manually modify the easy-install.pth .

Assuming that:

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

Then:

  • the file is located in a place like /home/user/workspace/empty/project/.venv/lib/python3.6/site-packages/easy-install.pth ;
  • its contents: /home/user/workspace/empty/project .

To make the import work properly, you can edit this line as follows:

  • /home/user/workspace/empty

Note:

  • Everything in /home/user/workspace/empty that looks like a Python package can be imported, so it’s a good idea to put the project in its own directory, in which case the empty directory contains nothing but the project directory.
  • The project.setup module is also imported.
+1
source

Using a symlink will not work (you should get recursive subdirectories), see my answer to this other post for an ugly hack, but which has the advantage of working with pip .

0
source

Source: https://habr.com/ru/post/1492403/


All Articles