A brief description of installing Python Packages Using Pip and Virtual Environments for WINDOWS.
pip
is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip
installed.
The Python installers for Windows
include pip. You can make sure that pip is up-to-date by running:
py -m pip install --upgrade pip
py -m pip --version
Afterwards, you should have the latest version of pip:
pip 23.3 from c:\python39\lib\site-packages
(Python 3.12)
'venv' (for 'Python 3') and 'virtualenv' (for 'Python 2') allow you to manage distinct package installations for various projects. They essentially allow you to construct a "virtual" isolated Python installation and install packages into that virtual installation. When switching projects, you may simply establish a new virtual environment and not worry about breaking the packages installed in the previous environments. It is usually suggested to use a virtual environment when building Python applications.
To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv
with virtualenv
in the below commands.
py -m venv env_name
The second argument is the location to create the virtual environment.
venv
will create a virtual Python installation in the env_name
folder.
Before you can start installing or using packages in your virtual environment you’ll need to activate
it. Activating a virtual environment will put the virtual environment-specific python
and pip
executables into your shell’s PATH
.
.\envAuto\Scripts\activate
You can confirm you’re in the virtual environment by checking the location of your Python interpreter:
where python
It should be in the env_name
directory:
...\env\Scripts\python.exe
As long as your virtual environment is activated
pip will install packages into that specific environment and you’ll be able to import and use packages in your Python application.
To install a package, use the following command:
pip install package_name
Specify the version using the following syntax:
pip install package_name==version_number
Keep packages up-to-date with:
pip install --upgrade package_name
Remove a package with:
pip uninstall package_name
Easily view the installed packages within your environment by using the following command:
pip list
This command provides a clear and concise list of all installed Python packages, along with their respective versions, giving you a quick overview of the components within your development environment.
Capture the exact versions of installed packages for reproducibility:
pip freeze > requirements.txt
To replicate the environment, use:
py -m pip install -r requirements.txt
Simply use:
deactivate
You should exclude your virtual environment directory from your version control system using .gitignore
.