🐍 Manage Python Dependencies [Pipenv]

Teepob Harutaipree
Bright Days
Published in
4 min readOct 25, 2020

Python is pretty popular these days, in order to work with others seamlessly are vital: Pipenv is used to manage the virtual environment of pip dependencies, kinda like package.json for node packages. Instead of generating node_modules file pipenv will create a new environment and install the required specified packages on the python projects.

Photo by Michael Dziedzic on Unsplash

> Installing

Check pip to ensure the correct version is used

pip -V

Install pipenv

pip install pipenv
brew install pipenv # Alternative for mac

>> For Linux : might need to add the binary to path <<

Run this command and see the output python -m site — user-base
Since I am using WSL (Ubuntu) the output I see is /home/tharutaipree/.local

open ~/.profile OR ~/.bash_profile and add the path output with /bin like this

… more detail here

Now you can use pipenv command !!

1> Create a new environment

Any of this will create the environment folder with python version specified

pipenv --two
pipenv --three
pipenv --python 3.6

You can set the environment to get the default version without and skip this step in the future export PIPENV_DEFAULT_PYTHON_VERSION=3.6

2> Generate the PipFile

Create new main.py and add some code with the following

main file to test the environment dependencies

Now registered these dependencies that are needed in main.py file into PipFile using:

pipenv install numpy pandas matplotlib

We get Pipfile and Pipfile.lock
Pipfile

Here you can see installed deps. are under [packages] and if you add the flag : — dev it will be under [dev-packages]

3> Create/Activate the pip environment on the current shell

pipenv shell
environment files created
  • After this command the .venev will be populated
  • Now execute python file normally using
python main.py
  • Now the code should run without error

We can check to see that we are in the current environment by using :
pip list && pip -V && python --version
Installed dependencies and python version is the same as in Pipfile

Pipfile.lock ensures that we use the current dependencies on the machine or saved by using pipenv lock so that everyone is working on the same version of the dependencies.

4> Pipfile ( More )

Uninstall the package from Pipfile
pip uninstall <package>

Update or generate Pipfile.lock from Pipfile
pipenv lock

Sync the dependencies of the environment from Pipfile.lock
pipenv sync

Update = lock + sync
pipenv update

5. Removing Virtualenv

You can decide to remove this virtualenv by using

To remove current virtual environment folder use
pipenv --rm
or manually remove on

PATH/virtualenvs/*
~/.local/share/virtualenvs

> Tips + Vscode

To exit pipenv
-
close and Run new shell OR hit Ctrl D

If you do not see the option try restart vscode or check for specific vscode settings if you still cannot see.

VSCODE

First, install python extensions HERE

Might need to reopen the window again after create a new environment

Use Pipenv in vscode

> Config Path

By default the virtualenv folder will be created inside some home path (PATH/virutalenvs/foldername-hashtoproductroot) with hash to the current path.

When PIPENV_VENV_IN_PROJECT is set the path is in /<currenct>/.venv

export PIPENV_VENV_IN_PROJECT=1

This will generate the .venv folder inside the project instead

Use WORKON_HOME to set to specific folder

export WORKON_HOME=~/.venv

References:

--

--

Teepob Harutaipree
Bright Days

💻 Full stack Developer who likes to exercise 🏊‍♂️and living the healthy lifestyles. 📑 Exploring and incorporating productivity tools in daily routine.