Python Programming Language: Creating A Python Virtual Environment For Beginners Python Virtual Environment Tutorial
Python Programming Language: Creating A Python Virtual Environment For Beginners Python Virtual Environment Tutorial
This tutorial will demonstrate how to create a virtual environment using the Python
programming language.
Before continuing, you need to download and install Python on your computer.
A virtual environment is an isolated operating space that prevents project packages from
overriding one another.
A Python virtual environment appears in a folder on your computer and contains all of the
necessary Python executable files and pip, the Python package manager.
To create a Python virtual environment, you need to access the built-in command line on your
computer and run the command to create a Python virtual environment. This command is
different in the macOS Terminal versus Windows Command Prompt.
The activate and deactivate commands are the keys to using a Python virtual environment
correctly.
Activating the environment is the only way to ensure that packages installed in the environment
only apply to the project in that folder.
Whereas, deactivating your virtual environment guarantees you won't accidentally install
packages in the incorrect virtual environment.
C:\Users\Owner> cd desktop
C:\Users\Owner\desktop>
Open the Windows Command Prompt enter into your Desktop folder with the command cd
desktop. You can find your device's command line interface (CLI) by searching in your
applications.>
C:\Users\Owner> cd desktop
C:\Users\Owner\desktop> py -m venv env
C:\Users\Owner\desktop> dir
...
...
05/13/2020 06:40 PM <DIR> env
When the environment is created, the prompt will reappear. To view the folder in the CLI
type dir for Windows and you will see it listed among your other Desktop files and folders.
How to activate a Python virtual environment in Windows
C:\Users\Owner\desktop> cd env
C:\Users\Owner\desktop\env> Scripts\activate
(env)C:\Users\Owner\desktop\env>
Before installing any packages, make sure to enter into the virtual environment and activate it.
Type cd env in the prompt then Scripts\activate. When activated, the terminal will
display (env) at the start of each line in the CLI.
(env)C:\Users\Owner\desktop\env> deactivate
C:\Users\Owner\desktop\env>
The last command you need to know when using a virtual environment is deactivate. When
deactivated, the command prompt will no longer display (env) before each line.
Always deactivate your virtual environment before moving to different directories or folders
outside of the project. If you forget, you run the chance of installing packages in the incorrect
environment.
Install pip
Usually Python3 comes with pip preinstalled. If you get an error "pip command not found", use
the following command to install pip:
cd Desktop
Execute get-pip.py
python get-pip.py
virtualenv
Launch virtualenv
cd your_project
virtualenv env
\env\Scripts\activate.bat
\path\to\env\Scripts\activate
Example:
C:\Users\'Username'\venv\Scripts\activate.bat
cd Desktop
Execute ez_setup.py:
python ez_setup.py
install pip:
easy_install pip
Your Python program and executable code can reside in any directory of your system,
therefore Operating System provides a specific search path that index the directories
Operating System should search for executable code.
The Path is set in the Environment Variable of My Computer properties:
To set path follow the steps:
In Variable name write path and in Variable value copy path up to C://Python(i.e., path where
Python is installed). Click Ok ->Ok.
8. Click on Ok button:
9. Click on Ok button: