Pipenv cheat sheet for npm users

I resisted using virtual environments in Python because they cause extra overhead and aren't project specific. On the other hand, I fell in love with npm at first sight, because it operates within the project directory and is aligned with actual developer objectives.

Pipenv cheat sheet for npm users
Loading the Elevenlabs Text to Speech AudioNative Player...
npm pipenv
./package.json ./Pipfile
./package-lock.json ./Pipfile.lock
./node_modules ./.venv
npm init pipenv --python 3
npm install pipenv install
npm install lodash@4.17 pipenv install pandas@1.1
npm install --save-dev lodash@4.17 pipenv install --dev pandas@1.1
npm update pipenv sync / pipenv update
npm update --dev pipenv sync --dev
npm ls pipenv graph
npm run pipenv run
# open a shell in virtual env pipenv shell
# end shell in virtual env exit
# save virtual env in current dir export PIPENV_VENV_IN_PROJECT=1

Why pipenv?

I initially resisted using virtual environments in Python due to their extra overhead and lack of project specificity. However, I fell in love with npm at first sight because it operates within the project directory and aligns with actual developer objectives. Pipenv is the npm equivalent for Python, offering a way to manage virtual environments without the hassle.

Quick start

  1. Install pipenv globally:

    pip install pipenv
    
  2. Configure pipenv to keep virtual environment files in the project directory (recommend adding to your shell init):

    export PIPENV_VENV_IN_PROJECT=1
    
  3. Create and enter a new project directory:

    mkdir pipenv-demo
    cd pipenv-demo
    
  4. Create a managed Python environment with desired version:

    pipenv --python 3
    
  5. Install a package (e.g., pandas) in the environment:

    pipenv install pandas
    

This will create a Pipfile in the directory:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pandas = "*"

[dev-packages]

[requires]
python_version = "3.6"

Both Pipfile and Pipfile.lock should be checked into version control. Another developer can reproduce the environment using:

pipenv install

Using the environment

  1. Activate the virtual environment:

    $ pipenv shell
    (pipenv-demo) $ which python
    /pipenv-demo/.venv/bin/python
    (pipenv-demo) $ python
    >>> import pandas
    >>> pandas.__version__
    '1.1.4'
    
  2. Run a Python file within the environment:

    $ echo 'import pandas; print(pandas.__version__)' > check.py
    $ pipenv run python check.py
    1.1.4
    

For official documentation, visit https://docs.pipenv.org/.

Other tricks

  1. Always set PIPENV_VENV_IN_PROJECT to emulate npm's behavior:

    export PIPENV_VENV_IN_PROJECT=1
    
  2. To reset the virtual environment:

    rm -r .venv
    
  3. A .env file is supported for specifying run-time environment variables.

Limitations of pipenv

  1. Lack of project automation features like npm start or the scripts section in package.json. Many power users resort to Makefile for automation.
  2. Configuring pipenv correctly in IDEs (PyCharm, VSCode, etc.) can be challenging.
  3. conda is a popular alternative that some developers prefer.

By using pipenv, you can streamline your Python development process and manage dependencies more efficiently, much like npm does for JavaScript projects.

Subscribe to Better Call Shao

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe