|npm init||pipenv --python 3|
|npm install||pipenv install|
|npm install firstname.lastname@example.org||pipenv install email@example.com|
|npm install --save-dev firstname.lastname@example.org||pipenv install --dev email@example.com|
|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|
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.
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).
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
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"
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.
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
$ 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/ .
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
To blow away the virtual environment for reasons (version conflict / out of disk) and start fresh, simply remove the
rm -r .venv
.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 the
packages.json. Last time I checked, most power users are using
Makefilefor automation. I don't hate
make, 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.
condais a popular alternative.