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 virutal env | pipenv shell |
# end shell in virutal env | exit |
# save virtual env in current dir | export PIPENV_VENV_IN_PROJECT=1 |
Why pipenv
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 is a npm equivalent for Python, a way out of having to think about virtual environments.
Quick start
First install pipenv from pip, and it'll be the only thing to install globally into the OS.
pip install pipenv
Configure pipenv to keep virtual environment files in the project directory. (I recommend adding this line to your shell init).
export PIPENV_VENV_IN_PROJECT=1
Then create a new directory for our project and change into it.
mkdir pipenv-demo
cd pipenv-demo
Create a managed Python environment with desired version.
pipenv --python 3
Install pandas
in the environment.
pipenv install pandas
Once finished, there should be 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"
Pipefile
and Pipfile.lock
(not shown here) work together to specify the information to reproduce this Python environment. Both files should be checked into version control, and another developer can use the install
command to reproduce the environment from these two files.
pipenv install
There are two ways to use the environment. First we can activate the underlying virtual environment with the shell
command. Once activated, the python
command will point to the executable in the environment and all of the installed libraries will be available.
$ pipenv shell
(pipenv-demo) $ which python
/pipenv-demo/.venv/bin/python
(pipenv-demo) $ python
>>> import pandas
>>> pandas.__version__
'1.1.4'
The second and more useful way is to have pipenv run our Python file with the run
command.
$ echo 'import pandas; print(pandas.__version__)' > check.py
$ pipenv run python check.py
1.1.4
Official doc is found here https://docs.pipenv.org/ .
Other tricks
I highly recommend always setting the PIPENV_VENV_IN_PROJECT
environment variable, making pipenv download files into .venv
under current directory, emulating npm's behavior of node_modules
.
export PIPENV_VENV_IN_PROJECT=1
To blow away the virtual environment for reasons (version conflict / out of disk) and start fresh, simply remove the .venv
directory.
rm -r .venv
A .env
file is also supported for specifying run-time environment variables.
Why not to use pipenv
- I wish pipenv has more project automation features like
npm start
, or thescripts
section inpackages.json
. Last time I checked, most power users are usingMakefile
for automation. I don't hatemake
, but maybe something more idiomatic? - I struggle to get pipenv configured correctly in IDEs (PyCharm, VSCode, etc), so I generally end up typing the pipenv commands instead of clicking the play button.
conda
is a popular alternative.