🐍 Manage Python Dependencies [Pipenv]
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.
> 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
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
Now registered these dependencies that are needed in main.py file into PipFile using:
pipenv install numpy pandas matplotlib
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
- 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 Pipfilepipenv lock
Sync the dependencies of the environment from Pipfile.lock pipenv sync
Update = lock + syncpipenv update
5. Removing Virtualenv
You can decide to remove this virtualenv by using
To remove current virtual environment folder usepipenv --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
> 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: