HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>You already know that pip installs packages in one of two environments:</p>
1 <p>You already know that pip installs packages in one of two environments:</p>
2 <ul><li>The system-wide environment</li>
2 <ul><li>The system-wide environment</li>
3 <li>The user environment</li>
3 <li>The user environment</li>
4 </ul><p>When importing a module or package, the Python interpreter looks for it first in the user environment, then in the system-wide environment. This sequence allows us to use the exact versions of the libraries and Python programs we need.</p>
4 </ul><p>When importing a module or package, the Python interpreter looks for it first in the user environment, then in the system-wide environment. This sequence allows us to use the exact versions of the libraries and Python programs we need.</p>
5 <p>But even two environments aren't enough when a programmer starts working on several projects because projects can have different dependencies.</p>
5 <p>But even two environments aren't enough when a programmer starts working on several projects because projects can have different dependencies.</p>
6 <p>And there are even more complex cases. Different projects may depend on one library but require different versions of it. That'll mean a version conflict.</p>
6 <p>And there are even more complex cases. Different projects may depend on one library but require different versions of it. That'll mean a version conflict.</p>
7 <p>A Python developer needs some mechanism to keep different projects in isolated sandboxes. There is a mechanism precisely for this. It's called<strong>virtual environments</strong>.</p>
7 <p>A Python developer needs some mechanism to keep different projects in isolated sandboxes. There is a mechanism precisely for this. It's called<strong>virtual environments</strong>.</p>
8 <h2>Designing virtual environments</h2>
8 <h2>Designing virtual environments</h2>
9 <p>Each virtual environment is a directory.</p>
9 <p>Each virtual environment is a directory.</p>
10 <p>Its contents are structurally similar to the system-wide environment - the subdirectories we named and populated accordingly. Let's look at an example:</p>
10 <p>Its contents are structurally similar to the system-wide environment - the subdirectories we named and populated accordingly. Let's look at an example:</p>
11 <p>The bin/ directory locates in the virtual environment directory. In the bin/ directory, you can find:</p>
11 <p>The bin/ directory locates in the virtual environment directory. In the bin/ directory, you can find:</p>
12 <ul><li>A copy of the interpreter named python3 (symbolic link to the original)</li>
12 <ul><li>A copy of the interpreter named python3 (symbolic link to the original)</li>
13 <li>A copy of the pip executable pip</li>
13 <li>A copy of the pip executable pip</li>
14 </ul><p>There are libraries already installed in the environment in the adjacent directory at lib/python3.6/site-packages. As a rule, a newly created environment has:</p>
14 </ul><p>There are libraries already installed in the environment in the adjacent directory at lib/python3.6/site-packages. As a rule, a newly created environment has:</p>
15 <ul><li>The pip package installed (the bin/pip - the executable is its entry point)</li>
15 <ul><li>The pip package installed (the bin/pip - the executable is its entry point)</li>
16 <li>The Setuptools package</li>
16 <li>The Setuptools package</li>
17 </ul><p>These two packages are the minimum for developing a project in Python.</p>
17 </ul><p>These two packages are the minimum for developing a project in Python.</p>
18 <p>When working in the environment, you shouldn't run the system Python and pip, but rather the executables from the bin directory. When the Python interpreter is in the environment, it knows where all available packages are. The interpreter finds them in the relative path ../lib/python3.6. A copy of pip from the bin/ directory installs packages in the same environment without affecting the system. It is the isolation we talked about at the beginning of the lesson.</p>
18 <p>When working in the environment, you shouldn't run the system Python and pip, but rather the executables from the bin directory. When the Python interpreter is in the environment, it knows where all available packages are. The interpreter finds them in the relative path ../lib/python3.6. A copy of pip from the bin/ directory installs packages in the same environment without affecting the system. It is the isolation we talked about at the beginning of the lesson.</p>
19 <h2>Creating a virtual environment</h2>
19 <h2>Creating a virtual environment</h2>
20 <p>There is no need to manually create the entire hierarchy of directories and files described above. There is a module called venv for that.</p>
20 <p>There is no need to manually create the entire hierarchy of directories and files described above. There is a module called venv for that.</p>
21 <p>On macOS and Windows, this module is in a bundle with Python. On Ubuntu, you have to install it separately with a command:</p>
21 <p>On macOS and Windows, this module is in a bundle with Python. On Ubuntu, you have to install it separately with a command:</p>
22 <p>Check that the module is installed and ready to use:</p>
22 <p>Check that the module is installed and ready to use:</p>
23 <p>Usually, we create the environment with the command python3 -m venv name_environment. Let's try to create a virtual environment and install the cowsay package there:</p>
23 <p>Usually, we create the environment with the command python3 -m venv name_environment. Let's try to create a virtual environment and install the cowsay package there:</p>
24 <p>We installed the package with an entry point, which we can call with the command first_venv/bin/cowsay. Also, the package becomes available to the interpreter we started in the environment. In this form, we can fully use the virtual environment. But constantly entering commands with the prefix first_venv/bin/ is inconvenient.</p>
24 <p>We installed the package with an entry point, which we can call with the command first_venv/bin/cowsay. Also, the package becomes available to the interpreter we started in the environment. In this form, we can fully use the virtual environment. But constantly entering commands with the prefix first_venv/bin/ is inconvenient.</p>
25 <p>There is a way to simplify calling the commands available in the environment. It's called<strong>activation</strong>.</p>
25 <p>There is a way to simplify calling the commands available in the environment. It's called<strong>activation</strong>.</p>
26 <h2>Activating the environment</h2>
26 <h2>Activating the environment</h2>
27 <p>When we created the environment, we placed a shell script in the bin subdirectory. We call it:</p>
27 <p>When we created the environment, we placed a shell script in the bin subdirectory. We call it:</p>
28 <ul><li>activate in macOS and Ubuntu</li>
28 <ul><li>activate in macOS and Ubuntu</li>
29 <li>activate.bat in Windows</li>
29 <li>activate.bat in Windows</li>
30 </ul><p>To execute this script, you need to call this command:</p>
30 </ul><p>To execute this script, you need to call this command:</p>
31 <ul><li><p>In macOS and Ubuntu:</p>
31 <ul><li><p>In macOS and Ubuntu:</p>
32 </li>
32 </li>
33 <li><p>In Windows:</p>
33 <li><p>In Windows:</p>
34 </li>
34 </li>
35 </ul><p>In the command above, the subdirectory with the executable files isn't called bin but Scripts.</p>
35 </ul><p>In the command above, the subdirectory with the executable files isn't called bin but Scripts.</p>
36 <p>In Ubuntu, the activation is as follows:</p>
36 <p>In Ubuntu, the activation is as follows:</p>
37 <p>After activation, you no longer need to specify the path to the called executable. Now Cowsay and Python are called without the prefix, but they're still the same commands from the environment.</p>
37 <p>After activation, you no longer need to specify the path to the called executable. Now Cowsay and Python are called without the prefix, but they're still the same commands from the environment.</p>
38 <p>The shell prompt has also changed to display the name of the environment. It works on macOS and Ubuntu and reminds us that we're in a virtual environment.</p>
38 <p>The shell prompt has also changed to display the name of the environment. It works on macOS and Ubuntu and reminds us that we're in a virtual environment.</p>
39 <p>We can deactivate the environment with the deactivate command, which becomes available after activation.</p>
39 <p>We can deactivate the environment with the deactivate command, which becomes available after activation.</p>
40 <p>Activation and deactivation only affect the current session, i.e., they're only visible in this terminal. It is convenient because you can have several environments and activate them simultaneously in different terminal windows this way.</p>
40 <p>Activation and deactivation only affect the current session, i.e., they're only visible in this terminal. It is convenient because you can have several environments and activate them simultaneously in different terminal windows this way.</p>
41 <p>Virtual environments are a powerful and convenient tool for isolating programs from each other and the system. Isolation even allows you to use different versions of Python in different environments. When working on projects of various ages, this is often vital! Therefore, as usual, we recommend adding virtual environments to the list of topics for more in-depth study in the future.</p>
41 <p>Virtual environments are a powerful and convenient tool for isolating programs from each other and the system. Isolation even allows you to use different versions of Python in different environments. When working on projects of various ages, this is often vital! Therefore, as usual, we recommend adding virtual environments to the list of topics for more in-depth study in the future.</p>