Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Virtual Environment | Virtual Environment
Mastering Python: Annotations, Errors and Environment
course content

Conteúdo do Curso

Mastering Python: Annotations, Errors and Environment

Mastering Python: Annotations, Errors and Environment

1. Annotations
2. Function Arguments in Details
3. Error Handling
4. Virtual Environment

bookCreating a Virtual Environment

To create venv in your project folder, you need just to write some simple commands in your Terminal, but describing every step to create your project will be greater.

Steps to create a project

Step 1. Create a folder for your project.

Step 2. Open the terminal in your folder.

For Windows:

  1. Open the project folder and copy the path to this folder in your file explorer.
  2. Open the Command Prompt (press Win + R, type "cmd", and press Enter).
  3. Type cd followed by a space, then paste the path to the project folder that you copied earlier.
    cd "your_path"
  4. Press Enter to change the current directory to the project folder.

Step 3. Create venv.
Run command:

Step 4. Activate scripts. Run command:

Description of Commands

Create virtual environment

The command python -m venv venv_name is used to create a virtual environment in Python.

Here is a breakdown of the command:

  • python or python3: This is the command to start the Python interpreter.
  • -m: This option allows you to run a Python module as a script. In this case, the venv module is used to create a virtual environment.
  • venv: This is the name of the module used to create the virtual environment.
  • venv_name: This is the name of the directory where the virtual environment will be created. The name venv is a convention for the virtual environment directory name, but you can choose a different name if you prefer.

Activate scripts

The command venv_name\Scripts\activate is used to activate a virtual environment named "venv_name" on a Windows operating system.
(source venv_name/bin/activate for Linux/MacOS`)

When you create a virtual environment using the venv module, a folder with the name of your environment is created in your project directory. Inside this folder, there is a subfolder named "Scripts" which contains scripts that can be used to activate or deactivate your virtual environment.

The command venv\Scripts\activate is used to activate your virtual environment. When you run this command in your command prompt or terminal, it sets the necessary environment variables and modifies your system's PATH variable to include the paths to the Python interpreter and packages installed in the virtual environment. This allows you to use the packages installed in the virtual environment instead of the system-wide packages.

When a virtual environment is activated, you can see its name in parentheses (venv_name) (in the terminal). It appears before the command prompt. Once you see this, you can install modules specifically for your project using pip.

Note

Remember, usually the name of the virtual environment used is venv (python -m venv venv), but it can also include the version of Python (in some cases): venv310 (for Python 3.10), venv37 (for Python 3.7), etc.

Structure

In the picture below, you can look at the structure of a new venv:

1. What command is used to create a Virtual Environment?
2. What command is used to activate a Virtual Environment?
What command is used to create a Virtual Environment?

What command is used to create a Virtual Environment?

Selecione algumas respostas corretas

What command is used to activate a Virtual Environment?

What command is used to activate a Virtual Environment?

Selecione algumas respostas corretas

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt