Conteúdo do Curso
Mastering Python: Annotations, Errors and Environment
Mastering Python: Annotations, Errors and Environment
Creating 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:
- Open the project folder and copy the path to this folder in your file explorer.
- Open the Command Prompt (press Win + R, type "cmd", and press Enter).
- Type
cd
followed by a space, then paste the path to the project folder that you copied earlier.cd "your_path"
- 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
orpython3
: 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, thevenv
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 namevenv
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:
Obrigado pelo seu feedback!