Setup.py: rename src package for project name

Let's say you have a project called proj , and in this project you have the following structure:

 proj/ dists/ doc/ src/ __init__.py xyz.py abc.py test/ setup.py 

As you can see, all the content of your project is located in the src subfolder. How to make distutils distribution package from src folder?

Following a tutorial , my naive idea was to write setup.py as follows:

 #omitting basics setup( name='proj', packages=['src'], package_dir={'proj':'src'} ) 

But after installing the resulting package in my system, I still have to import src.xyz , not proj.xyz , which would be the goal and the expected result.

+7
source share
3 answers

You can fix this by putting the Python package files in the proj/ directory:

 proj/ src/ proj/ __init__.py xyz.py abc.py setup.py 

And changing setup.py to:

 # ... setup( name='proj', packages=['proj'], package_dir={'':'src'} ) 

This is not required by distutils, but other tools may expect the parent directory name of the __init__.py file to be the same as the name of the Python package, i.e. proj in this case.

+4
source

This is due to the error in setuptools described here: https://github.com/pypa/setuptools/issues/250

Basically, it works, but not in dev mode. You now have 3 solutions:

  • symbolically binds the src package as proj (and ignore it when it arrives), it will work out of the box, but dirty
  • change from src to proj
  • create the proj subdirectory in src and use the following parameters:
  packages = ['proj'],
 package_dir = {'proj': 'src / proj'},
+2
source

You can try adding the src folder to PYTHONPATH before calling the setup function:

 import sys, os src_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'src') sys.path.append(src_path) 

And also, to be safe, you then change the working directory:

 os.chdir(src_path) 

After that, everything should be in order.

Some other packaging tools support your application from the inside out. I thought it was setuptools, it turns out PyInstaller. But basically, what you need to do is enough for your packages to be imported directly.

Turns out distutils has a package_dir directive. This is what you should use, but it can only work by adding your package to PYTHONPATH .

-3
source

All Articles