A virtual environment in Python is a self-contained directory that holds your project's Python interpreter, libraries, and scripts. This setup ensures that each project has its own dependencies, regardless of the dependencies of other projects. This isolation prevents conflicts between projects and makes dependency management much easier.
First, install Python 3, pip, and the virtual environment package. Open your terminal and run:
sudo apt install python3-pip python3-venv
To create a new virtual environment, use the following commands. This example creates a virtual environment for a project called "telegram-client":
mkdir -p ~/.venv/telegram-client
python3 -m venv ~/.venv/telegram-client
Before you start working on your project, you need to activate the virtual environment. To do this, run:
source ~/.venv/telegram-client/bin/activate
Once activated, your terminal prompt will change to indicate that you are now working inside the virtual environment.
If you have multiple Python versions installed and want to create a virtual environment with a specific Python version, you can specify the Python interpreter. For example, to create a virtual environment with Python 3.8, you would use:
python3.8 -m venv ~/.venv/telegram-client
Ensure that the desired Python version is installed on your system. You can install a specific Python version using a package manager or by downloading it from the Python website.
After activating the virtual environment, you can install packages using pip
. For example, to work with LonamiWebs/Telethon, you can clone the repository and install its requirements:
git clone https://github.com/LonamiWebs/Telethon.git
cd Telethon
pip install -r requirements.txt
Alternatively, you can install Telethon directly from PyPI using:
pip install telethon
Remember to deactivate the virtual environment when you're done working on your project by running:
deactivate
This guide should help you set up a Python 3 virtual environment and manage dependencies for your projects more effectively.