Search code examples
pythonpipenv

Developing a Python library with Pipenv


I'm trying to develop a Python library which will eventually be put on PyPI.

It's a library I use in another project which pulls it from PyPI.

I have unit-tests for the library in its own project repository. But I mainly test the library in use through the main application.

I was previously "publishing" the library locally, using

pip install -e

so that the main project in another repository can pull it from local packages and I can test it in context.

But now I'm moving to pipenv. And I want to be able to do the same. But if I put the dependency in the Pipenv file, it seems to try to pull from the real PyPI, not my local store.

How do I set up this workflow with Pipenv?


Solution

  • Pipenv can install packages from various sources, not only from PyPI. The CLI of pipenv is very similar to pip by design, because many arguments of pipenv install are internally piped to pip install. You can use a local path or a URL with VCS prefix as package specifier with either pipenv or pip.

    CLI Usage

    First go to the project folder (which contains the Pipfile) of your main application. Then run

    $ pipenv install --dev -e "/path/to/your/local/library"
    

    If the library is version controlled by Git or SVN, you can also use an URL like this:

    $ pipenv install --dev -e git+https://github.com/your_user_id/libraryname@develop
    

    If the Git repository for your library is stored locally, use file:// instead of https://github.com. Other protocols like FTP and SSH are also supported.

    The above command will pull the package from the source, install it and modify the Pipfile in the current folder to include the package.

    Pipfile usage

    Usually you do not need to modify the Pipfile directly. For advanced settings in the pipfile, please see the Pipfile's specs. Below are some example entries to pipfile

    [dev-packages]
    mylibrary = { git = 'https://github.com/xxx/mylibrary.git', ref = '0.0.1', editable = true }
    "e1839a8" = {path = "/path/to/your/local/library2", editable = true}
    "e51a27" = {file = "/path/to/your/local/library1/build/0.0.1.zip"}
    

    Setup a private PyPI index

    Although it would be overkill, just to be complete, setting up a private PyPI server can also work.