Search code examples
pythondistutilssetup.py

How do I distribute all contents of root directory to a directory with that name


I have a project named myproj structured like

/myproj
    __init__.py
    module1.py
    module2.py
    setup.py

my setup.py looks like this

from distutils.core import setup

setup(name='myproj',
      version='0.1',
      description='Does projecty stuff',
      author='Me',
      author_email='me@domain.com',
      packages=[''])

But this places module1.py and module2.py in the install directory.


How do I specify setup such that the directory /myproj and all of it's contents are dropped into the install directory?


Solution

  • In your myproj root directory for this project, you want to move module1.py and module2.py into a directory named myproj under that, and if you wish to maintain Python < 3.3 compatibility, add a __init__.py into there.

    ├── myproj
    │   ├── __init__.py
    │   ├── module1.py
    │   └── module2.py
    └── setup.py
    

    You may also consider using setuptools instead of just distutils. setuptools provide a lot more helper methods and additional attributes that make setting up this file a lot easier. This is the bare minimum setup.py I would construct for the above project:

    from setuptools import setup, find_packages
    
    setup(name='myproj',
          version='0.1',
          description="My project",
          author='me',
          author_email='me@example.com',
          packages=find_packages(),
          )
    

    Running the installation you should see lines like this:

    copying build/lib.linux-x86_64-2.7/myproj/__init__.py -> build/bdist.linux-x86_64/egg/myproj
    copying build/lib.linux-x86_64-2.7/myproj/module1.py -> build/bdist.linux-x86_64/egg/myproj
    copying build/lib.linux-x86_64-2.7/myproj/module2.py -> build/bdist.linux-x86_64/egg/myproj
    

    This signifies that the setup script has picked up the required source files. Run the python interpreter (preferably outside this project directory) to ensure that those modules can be imported (not due to relative import).

    On the other hand, if you wish to provide those modules at the root level, you definitely need to declare py_modules explicitly.

    Finally, the Python Packaging User Guide is a good resource for more specific questions anyone may have about building distributable python packages.