Python packages are installed with pip command. Packages can be found at pypi.org
To install pip on you system, run command:
sudo apt-get install python3-pip
To upgrade pip run:
pip install --upgrade pip
To install packages with pip, use command:
pip install package_name
To uninstall packages with pip, use command:
pip uninstall package_name
To list all the packages installed with pip:
pip list
Pip freeze command (that also lists pip packages) is often used to print out fixed versions or minimum versions of pip packages for some project. For example, if we are building some Python project and we have set the Python virtual environment for it and installed all the required pip packages inside. we can print out the list of the to a .txt file to share with others who will work on that project:
pip freeze > requirements.txt
We can use freeze command to print out directly to the Terminal instead to a file:
pip freeze
Pip wheels are the part of pip that help in installation of packages and make it faster. Install pip wheels, run command:
pip install wheel setuptools
We can install both pip and wheels and upgrade all packages (-U) with the command:
python3 -m pip install -U pip wheel setuptools
-U is the same as –upgrade
To install pip packages in bulk, save it in the .txt file where every package's name is separate row. Example of the .txt file contents:
pyautogui
selenium
pytesseract
mouseinfo
requests
pytest
pytest-html
opencv-python
pandas
When you create the file, run the command from the terminal to install all of the packages:
pip install -r path/to/requirements.txt
If you want install all of the same packages on the second computer, just run on the command on the first computer to save the list of all the packages:
pip freeze > requirements.txt
To upgrade all pip packages on your computer, run:
pip freeze > requirements.txt
to get the list of all packages. The list be like like in the example:
PyAutoGUI==0.9.53
selenium==4.1.0
pytesseract==0.3.8
MouseInfo==0.1.3
requests==2.27.1
pytest==7.0.1
pytest-html==3.1.1
opencv-python==4.5.5.62
pandas==1.4.1
Now use Find and Replace option in you text editor to replace all == to >= , save it and than run:
pip install -r path/to/requirements.txt --upgrade
If venv (Virtual Environment) is not installed with Python on the system install it by running the command:
sudo apt install python3-venv
To create venv in Linux in VS Code, run command in Console:
python3 -m venv /path/to/new/virtual/environment_name
("-m" stands for module, and we use it if we want to run module directly) or if your Terminal is already opened from the path where you want venv to be, just type:
python3 -m venv environment_name
To activate venv run command:
source path/to/new/virtual/environment_name/bin/activate
or if your Terminal is already opened from the path where you want venv to be, just type:
source environment_name/bin/activate
To deactivate venv just type:
deactivate
When installing pip packages in venv. do not use sudo because if you do, you are escaping virtual environment and installing packages as a root.
If you rename the folder of your project where venv is created, you'll have to adjust some variables from some files inside the bin directory of your project.
For example:
bin/pip
bin/activate