bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Python/Foundations
Python•Foundations

Python Virtual Environment

What is a Virtual Environment?

A virtual environment in Python is an isolated environment on your computer, where you can run and test your Python projects.

It allows you to manage project-specific dependencies without interfering with other projects or the original Python installation.

Think of a virtual environment as a separate container for each Python project. Each container:

  • Has its own Python interpreter
  • Has its own set of installed packages
  • Is isolated from other virtual environments
  • Can have different versions of the same package

Using virtual environments is important because

  • It prevents package version conflicts between projects
  • Makes projects more portable and reproducible
  • Keeps your system Python installation clean
  • Allows testing with different Python versions

Creating a Virtual Environment

Python has the built-in venv module for creating virtual environments.

To create a virtual environment on your computer, open the command prompt, and navigate to the folder where you want to create your project, then type this command:

Example

Run this command to create a virtual environment named myfirstproject :

C:\Users\
Your Name
>
python -m venv myfirstproject
$
python -m venv myfirstproject

This will set up a virtual environment, and create a folder named "myfirstproject" with subfolders and files, like this:

Result

The file/folder structure will look like this:

myfirstproject
Include
Lib
Scripts
.gitignore
pyvenv.cfg

Activate Virtual Environment

To use the virtual environment, you have to activate it with this command:

Activate the virtual environment

C:\Users\
Your Name
>
myfirstproject\Scripts\activate
$
source myfirstproject/bin/activate

After activation, your prompt will change to show that you are now working in the active environment:

Result

The command line will look like this when the virtual environment is active:

(myfirstproject) C:\Users\
Your Name
>
(myfirstproject) ... $

Install Packages

Once your virtual environment is activated, you can install packages in it, using pip .

We will install a package called 'cowsay':

Install 'cowsay' in the virtual environment

(myfirstproject) C:\Users\
Your Name
>
pip install cowsay
(myfirstproject) ... $
pip install cowsay

Result

'cowsay' is installed only in the virtual environment:

Collecting cowsay
Downloading cowsay-6.1-py3-none-any.whl.metadata (5.6 kB)
Downloading cowsay-6.1-py3-none-any.whl (25 kB)
Installing collected packages: cowsay
Successfully installed cowsay-6.1
[
notice
] A new release of pip is available:
 25.0.1
 ->
 25.1.1
 [
 notice
 ] To update, run:
 python.exe -m pip install --upgrade pip

Using Package

Now that the 'cowsay' module is installed in your virtual environment, lets use it to display a talking cow.

Create a file called test.py on your computer. You can place it wherever you want, but I will place it in the same location as the myfirstproject folder -not in the folder, but in the same location.

Open the file and insert these three lines in it:

Example

Insert two lines in test.py :

import cowsay
cowsay.cow("Good Mooooorning!")

Then, try to execute the file while you are in the virtual environment:

Example

Execute test.py in the virtual environment:

(myfirstproject) C:\Users\
Your Name
>
python test.py
(myfirstproject) ... $
python test.py

As a result a cow will appear in you terminal:

Result

The purpose of the 'cowsay' module is to draw a cow that says whatever input you give it:

_________________
| Good Mooooorning! |
=================
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
_________________
| Good Mooooorning! |
=================
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||

Deactivate Virtual Environment

To deactivate the virtual environment use this command:

Deactivate the virtual environment

(myfirstproject) C:\Users\
Your Name
>
deactivate
(myfirstproject) ... $
deactivate

As a result, you are now back in the normal command line interface:

Normal command line interface

C:\Users\
Your Name
>
$

If you try to execute the test.py file outside of the virtual environment, you will get an error because 'cowsay' is missing. It was only installed in the virtual environment:

Example

Execute test.py outside of the virtual environment:

C:\Users\
Your Name
>
python test.py
$
python test.py

Error because 'cowsay' is missing

Traceback (most recent call last):
 File
 "C:\Users\
 Your Name
 \test.py"
 , line
 , in
 <module>
 import cowsay
 ModuleNotFoundError
 No module named 'cowsay'

Note

The virtual environment myfirstproject still exists, it is just not activated. If you activate the virtual environment again, you can execute the test.py file, and the diagram will be displayed.

Delete Virtual Environment

Another nice thing about working with a virtual environment is that when you, for some reason want to delete it, there are no other projects depend on it, and only the modules and files in the specified virtual environment are deleted.

To delete a virtual environment, you can simply delete its folder with all its content. Either directly in the file system, or use the command line interface like this:

Example

Delete myfirstproject from the command line interface:

C:\Users\
Your Name
>
rmdir /s /q myfirstproject
$
rm -rf myfirstproject

Previous

Python User Input

Next chapter

Object-Oriented Python

Start with Python OOP

This chapter

Overview
100

Lessons

265m

Read time

1. Python Tutorial2. Python Introduction3. Python Getting Started4. Python Syntax5. Python Statements6. Python Output / Print7. Python Output Numbers8. Python Comments9. Python Variables10. Python - Variable Names11. Python Variables - Assign Multiple Values12. Python - Output Variables13. Python - Global Variables14. Python Data Types15. Python Numbers16. Python Casting17. Python Strings18. Python - Slicing Strings19. Python - Modify Strings20. Python - String Concatenation21. Python - Format - Strings22. Python - Escape Characters23. Python - String Methods24. Python Booleans25. Python Operators26. Python Arithmetic Operators27. Python Assignment Operators28. Python Comparison Operators29. Python Logical Operators30. Python Identity Operators31. Python Membership Operators32. Python Bitwise Operators33. Python Operator Precedence34. Python Lists35. Python - Access List Items36. Python - Change List Items37. Python - Add List Items38. Python - Remove List Items39. Python - Loop Lists40. Python - List Comprehension41. Python - Sort Lists42. Python - Copy Lists43. Python - Join Lists44. Python - List Methods45. Python Tuples46. Python - Access Tuple Items47. Python - Update Tuples48. Python - Unpack Tuples49. Python - Loop Tuples50. Python - Join Tuples51. Python - Tuple Methods52. Python Sets53. Python - Access Set Items54. Python - Add Set Items55. Python - Remove Set Items56. Python - Loop Sets57. Python - Join Sets58. Python frozenset59. Python - Set Methods60. Python Dictionaries61. Python - Access Dictionary Items62. Python - Change Dictionary Items63. Python - Add Dictionary Items64. Python - Remove Dictionary Items65. Python - Loop Dictionaries66. Python - Copy Dictionaries67. Python - Nested Dictionaries68. Python Dictionary Methods69. Python If Statement70. Python Elif Statement71. Python Else Statement72. Python Shorthand If73. Python Logical Operators74. Python Nested If75. Python Pass Statement76. Python Match77. Python While Loops78. Python For Loops79. Python Functions80. Python Function Arguments81. Python *args and **kwargs82. Python Scope83. Python Decorators84. Python Lambda85. Python Recursion86. Python Generators87. Python range88. Python Arrays89. Python Iterators90. Python Modules91. Python Datetime92. Python Math93. Python JSON94. Python RegEx95. Python PIP96. Python Try Except97. Python String Formatting98. Python None99. Python User Input100. Python Virtual Environment

On this page

What is a Virtual Environment?Creating a Virtual EnvironmentActivate Virtual EnvironmentInstall PackagesUsing PackageDeactivate Virtual EnvironmentDelete Virtual Environment