[go: up one dir, main page]

0% found this document useful (0 votes)
64 views9 pages

Python Programming Language: Creating A Python Virtual Environment For Beginners Python Virtual Environment Tutorial

Creating a Python virtual environment allows isolating package installations for different projects. This tutorial explains that a virtual environment contains necessary Python files and pip, and is created using the py -m venv command. It must be activated using activate/deactivate commands before installing packages or coding to ensure packages only apply to that project folder. The tutorial demonstrates these steps for Windows and also covers installing pip if needed.

Uploaded by

James Ngugi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views9 pages

Python Programming Language: Creating A Python Virtual Environment For Beginners Python Virtual Environment Tutorial

Creating a Python virtual environment allows isolating package installations for different projects. This tutorial explains that a virtual environment contains necessary Python files and pip, and is created using the py -m venv command. It must be activated using activate/deactivate commands before installing packages or coding to ensure packages only apply to that project folder. The tutorial demonstrates these steps for Windows and also covers installing pip if needed.

Uploaded by

James Ngugi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

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\activate.  When 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 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

Pip (Python Package Installer)

Usually Python3 comes with pip preinstalled. If you get an error "pip command not found", use
the following command to install pip:

Download get-pip.py, make sure you're saving file to Desktop

In your Command Prompt navigate to Desktop

cd Desktop
Execute get-pip.py

python get-pip.py

Now pip should work system wide.

virtualenv

In your Command Prompt enter:

pip install virtualenv

Launch virtualenv

In your Command Prompt navigate to your project:

cd your_project

Within your project:

virtualenv env

Activate your virtualenv:

on Windows, virtualenv creates a batch file

\env\Scripts\activate.bat

to activate virtualenv on Windows, activate script is in the Scripts folder :

\path\to\env\Scripts\activate

Example:

C:\Users\'Username'\venv\Scripts\activate.bat

Another way to install pip

Save the "ez_setup.py" file to your desktop form https://bootstrap.pypa.io/ez_setup.py

In your Command Prompt navigate to Desktop:

cd Desktop

Execute ez_setup.py:
python ez_setup.py

install pip:

easy_install pip

SETTING PATH IN PYTHON

Before starting working with Python, a specific path is to set.

 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:

Right click on My Computer ->Properties ->Advanced System setting ->Environment Variable


->New

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.

Path will be set for executing Python programs.

1. Right click on My Computer and click on properties.

2. Click on Advanced System settings


3. Click on Environment Variable tab.

4. Click on new tab of user variables.


5. Write path in variable name

6. Copy the path of Python folder


7. Paste path of Python in variable value.

8. Click on Ok button:
9. Click on Ok button:

You might also like