Creating a Python Virtual Environment for Beginners

May 13, 2020, 5:44 p.m.

Beginners Python · 4 min read

Creating a Python Virtual Environment for Beginners

Last Modified: Sept. 15, 2020, 2:28 p.m.

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.

 

What is a virtual environment?

A virtual environment is an isolated operating space that prevents project packages from overriding one another.

 

What is a Python virtual environment?

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.

 

How to create a Python virtual environment

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. 

 

How to use a Python virtual environment

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.

Before you begin coding any project, create a virtual environment.

 


 

Python virtual environment Windows

Open a command line interface (CLI)

Windows Command Prompt

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.

 

Creating a Python virtual environment in Windows

Windows Command Prompt

C:\Users\Owner> cd desktop
C:\Users\Owner\desktop> py -m venv env

Type py -m venv env to create a virtual environment named env

 

View the virtual environment

Windows Command Prompt

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

Windows Command Prompt

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\activateWhen activated, the terminal will display (env) at the start of each line in the CLI.

 

Deactivate the virtual environment

Windows Command Prompt

(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 deactivateWhen 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.

 


 

Python virtual environment macOS

Open a command line interface (CLI)

macOS Terminal

User-Macbook:~ user$ cd desktop
User-Macbook:desktop user$

Search in your applications for the Terminal and open it. Enter into your Desktop folder with the command cd desktop

 

Creating a Python virtual environment

macOS Terminal

User-Macbook:~ user$ cd desktop
User-Macbook:desktop user$ python3 -m venv env

Type python3 -m venv env to create a virtual environment named env

 

View the virtual environment

macOS Terminal

User-Macbook:desktop user$ ls

...   ...   env

When the environment is created, the prompt will reappear. To view the folder in the CLI type ls to see the Python virtual environment listed among your other Desktop files and folders.

 

How to activate a Python virtual environment in macOS

macOS Terminal

User-Macbook:desktop user$ cd env

User-Macbook:env user$ source bin/activate

(env)User-Macbook:env user$

Before installing any packages, make sure to enter into the virtual environment and activate it. Type cd env in the prompt then source bin/activate. When activated, the terminal will display (env) at the start of each line in the CLI.

 

Deactivate the virtual environment

macOS Terminal

(env)User-Macbook:env user$ deactivate

User-Macbook:env user$

The last command you need to know when using a virtual environment is deactivateWhen 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.

 


 

Next steps...

With your Python virtual environment created, you can now move on to using the Django Web Framework

Django rocketship


0
Subscribe now

Subscribe to stay current on our latest articles and promos





Post a Comment
Join the community

0 Comments