Python package manager

PIP

pip is a package manager for python which installs packages globally in your machine in /Library directory. pip is included in python 3 installation and you don't need to install it manually. If you install python3 and you already have python installed on your machine, maybe you need to use python3 and pip3 commands. Since pip installs packages globally, you need to install a virtual environment such as pipenv if you want complete isolation which is not dependent on the machine it's running on.

install, upgrade, uninstall

pip install imdbpy
pip install package-name==1.0.0
pip install package-name --upgrade
pip uninstall package-name
pip search imdbpy

Package Details

pip show package-name

List

pip list
pip list --outdated

Pipenv

Pipenv is the dependency manager recommended by python. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases. pipenv creates a folder outside of the project and copies all the packages into that folder. It maintains a dependency list file called pipfile and by running pipenv install it restores all the packages in pipfile.

  1. Install pipenv using pip:
pip install pipenv
  1. Create a new environment (it looks for a pipfile, if it doesn’t exist, it will create a new one):
pipenv install
  1. Add packages to the project:
pipenv install package-name
  • Creates a dependency file called Pipfile in which all the packages and dependencies are listed there. There is also a Pipfile.lock which keeps the versions of all dependencies.

  • Virtual environment location is in C:\Users\Soheil\.virtualenvs\ and the source code of packages could be found in Lib\site-packages.

  • The name of this virtual environment comes from the project root directory name (project name) plus the hash of the full path to the project root. This guarantees names are unique and predictable as long as you don't move the project.

  • For running applications, we can either run pipenv run py main.py which is like npm run and it will ensure that your installed packages are available to your script. Or we can run pipenv shell which activates a sub-shell and we can run the application normally by py main.py. for exiting the subshell run exit command.

  • If you try to run the application by py main.py when the shell is not active, you will encounter dependency errors since those dependencies are not available globally or in the application folder, so the running should happen via pipenv pipeline.

Create a new Project using pipenv in Visual Studio Code

  1. Create a pipfile:
pipenv install
  1. Create main.py file and put your code and import your dependencies
  2. Open the folder in VSCode and because dependencies are in pipenv you will see that linter is complaining about your imports.
  3. ctrl + shift + p -> Python: Select Interpreter and select the virtual environment for your project. It'll fix your import linting issue.
  4. For running the script use pipenv run python main.py because you want to use pipenv for running your script. Your dependencies are not available outside the virtual environment so `python main.py`` will fail.

Virtualenv (Obsolete)

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder that contains all the necessary executables to use the packages that a Python project would need. This approach creates a folder called venv inside the project folder and when we install a package it copies dependencies into venv/Lib/site-packages.

install:

pip install virtualenv

create a virtual environment for a project:

cd project_folder
virtualenv venv

virtualenv venv will create venv folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv) can be anything; omitting the name will place the files in the current directory instead.

venv is the general convention used globally. As it is readily available in .gitignore.

to begin using the virtual environment, it needs to be activated first. The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$) to let you know that it’s active.

From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

activate in Mac:

$ source venv/bin/activate

activate in Windows:

$ venv\Scripts\activate

When venv is activated we can install packages normally like pip install requests and it will consider the current environment as a standalone machine. for running the app also we can simply run py main.py when the virtual environment is active.

if we want to deactivate the environment we can run deactivate.

Please note that in this way of working there is no package list available in the project, because we only installed packages and they have been copied into our venv folder but there is no list like packages.json in npm which stores all packages with their version. In order to keep a list of required packages in each project we need to create a file called requirements.txt and list all the packages in that file.

For creating a requirement file we can use pip freeze > requirements.txt.

When we push the project we only commit source files and this requirement.txt file because venv folder has been excluded in the .gitignore file. When we want to start the project in another system, we pull the code and create a virtual environment, then we install all the dependencies via pip install -r requirements.txt.