Search code examples
pythongitpackagereusability

one python project with multiple packages


I'm moving on from single scripts to a bigger python application.

It's an application with multiple packages.

package1-> package1/.py files

package2-> package2/.py files

As package 1 should be able to be used stand alone, I keep it in a separate git repo. I'd love to do in package2: import package1

It feels like the easiest way to do it would be having project1 (in its git repo) in a subdirectory of project2, but that doesnt sound like a nice solution.

Some answers I found feels dated and I couldn't get it to work. (python setup.py install)

Adding package1 location to the PATH is a solution, but it's not very nice if I want to distribute it to co-workers. Ideally, I "install" the package as easily as possible.

I read "pip" would be preferred, but would need some directions where to start looking for creating a package. Also, distribution would be only local.

(python3.6. Code will be used on linux and windows. )


Solution

  • excerpt from an excellent (but kindof hidden) answer using pip given by np8 in question Importing modules from parent folder: checkout his answer!

    --

    1) Add a setup.py to the root folder

    The contents of the setup.py can be simply

    from setuptools import setup, find_packages
    
    setup(name='myproject', version='1.0', packages=find_packages())
    

    Basically "any" setup.py would work. This is just a minimal working example.

    2) Use a virtual environment

    3) pip install your project in editable state

    Install your top level package myproject using pip. The trick is to use the -e flag when doing the install. This way it is installed in an editable state, and all the edits made to the .py files will be automatically included in the installed package.

    In the root directory, run

    pip install -e . (note the dot, it stands for "current directory")

    You can also see that it is installed by using pip freeze