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.
| 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
-
Install pipenv globally:
pip install pipenv -
Configure pipenv to keep virtual environment files in the project directory (recommend adding to your shell init):
export PIPENV_VENV_IN_PROJECT=1 -
Create and enter a new project directory:
mkdir pipenv-demo cd pipenv-demo -
Create a managed Python environment with desired version:
pipenv --python 3 -
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
-
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' -
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
-
Always set
PIPENV_VENV_IN_PROJECTto emulate npm's behavior:export PIPENV_VENV_IN_PROJECT=1 -
To reset the virtual environment:
rm -r .venv -
A
.envfile is supported for specifying run-time environment variables.
Limitations of pipenv
- Lack of project automation features like
npm startor thescriptssection inpackage.json. Many power users resort toMakefilefor automation. - Configuring pipenv correctly in IDEs (PyCharm, VSCode, etc.) can be challenging.
condais 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.