Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setup.py: renaming src package to 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 in the src subfolder. How to go about making a distutils distribution package out of the src folder?

My naive idea, following the tutorial, would've been to write the setup.py like this:

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

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

like image 210
erikbstack Avatar asked Jan 19 '13 18:01

erikbstack


2 Answers

You could fix it by putting Python package files into 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'}
)

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

like image 190
jfs Avatar answered Sep 22 '22 09:09

jfs


This is due to a bug in setuptools reported here: https://github.com/pypa/setuptools/issues/250

Basically, it does work but not in dev mode. Now on you have 3 solutions:

  • symlink the src package as proj (and ignore it when comitting), it will works out of the box but is dirty
  • change from src to proj
  • create a subdirectory proj in src and use the following options:
packages=['proj'],
package_dir={'proj': 'src/proj'},
like image 36
IxDay Avatar answered Sep 20 '22 09:09

IxDay