Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create and Use Python Virtual Environments with python3-venv in Linux

Python virtual environments are a powerful tool for managing dependencies and isolating Python projects. They allow you to create an isolated environment with its own Python interpreter and installed packages, separate from the system-wide Python installation. This is particularly useful when working on multiple projects with different requirements, as it helps avoid conflicts between packages.

In the Linux environment, the python3-venv module is the recommended way to create and manage virtual environments. It is a built-in module in Python 3, so there is no need to install any additional packages.

To create a virtual environment using python3-venv, you can follow these steps:

  1. Open a terminal.

  2. Navigate to the directory where you want to create the virtual environment.

  3. Run the following command to create a new virtual environment:

    python3 -m venv myenv

    This will create a new directory called "myenv" which will contain the virtual environment.

  4. Activate the virtual environment by running the following command:

    source myenv/bin/activate

    After activation, your terminal prompt will change to indicate that you are now working within the virtual environment.

  5. You can now install packages and run Python scripts within the virtual environment. Any packages you install will be isolated to the virtual environment and will not affect the system-wide Python installation.

  6. To deactivate the virtual environment and return to the system-wide Python environment, simply run the following command:

    deactivate

    Your terminal prompt will revert back to the original state.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.