Search code examples
pythonsetuptools

Installing a local Python package dynamically


I'm building a small Python package with a friend.

.
├── __init__.py
└── memoized.py

During the development process, I want to test the installed package - for example, to make sure that some __init__.py code runs as expected.

The current way to achieve this is:

  1. Create a virtualenv on /tmp
  2. Activate it
  3. cd to the repo dir
  4. python setup.py install
  5. cd elsewhere (to avoid importing from the directory instead of importing from the installed package)
  6. Test the import
  7. pip uninstall memoized
  8. Repeat steps 3-7

This is long and cumbersome, and I vaguely remember there's a way to install a package in a way that does not copy its code to the virtualenv, but links to it instead. This way changing the code would be reflected at the next import, without re-installing.

I've searched for "Dynamic importing" and alike but it refers to a different feature.

How can I install a local Python package so that changes to the package code would be immediately reflected?


Solution

  • You could activate the virtualenv first and navigate to the project directory. Then run

    pip install --editable . 
    

    --editable install a project in editable mode and uses current working directory instead of copying the source code.