• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / How to Use Python virtualenv

How to Use Python virtualenv

Author: PFB Staff Writer
Last Updated: January 4, 2023

While programming, we sometimes make errors that can make undesirable changes to our system. To avoid this, we often use virtual environments. In this article, we will discuss how to create and use a virtual environment using the virtualenv module in python.

Table of Contents
  1. What is a Virtual Environment?
  2. Install virtualenv and Verify the Installation
  3. Install Virtualenv in Linux Ubuntu
  4. Setup and Use Virtualenv in Linux
  5. What Does Virtualenv do?
  6. Install a Package in your Virtualenv
  7. Conclusion

What is a Virtual Environment?

A Virtual Environment put simply, is an isolated working copy of Python that allows you to work on a specific project without worry of affecting other projects.

A virtual environment enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. In python, we use virtual environments using the virtualenv module.

Install virtualenv and Verify the Installation

There is a chance that python virtualenv is already installed on your system. To check if it’s already installed run the following command in your terminal.

virtualenv --version

After execution of the above command, If you see a version number (in my case 1.6.1), it’s already installed on your system.
>>1.6.1

If virtualenv is not installed on your system, you can install it in Linux using the commands discusses in the following section

Install Virtualenv in Linux Ubuntu

There are a number of ways to install virtualenv on your system.

To install virtualenv using the apt package management system, you can use the following command.

sudo apt-get install python-virtualenv

If you want to install python virtualenv using the easy_install package installer, you can use the following command.

sudo easy_install virtualenv

If can also use python’s PIP to install virtualenv using the following command.

sudo pip install virtualenv

If you are using python3, you can use pip3 instead of pip in the above command.,

Setup and Use Virtualenv in Linux

Once you have virtualenv installed on your system, just fire up a shell and create your own environment using the following steps.

  • First, create a directory for your new shiny isolated environment. For this, you can use the mkdir command and give the full pathname of the directory to be created. For instance, to create a folder named virtualenvironment in the home directory, you can use the following command.
mkdir ~/virtualenvironment
  • After creating the folder for the virtual environment, we will create a folder for our python execution using virtualenv. To create a folder for your new app that includes a clean copy of Python, you can use the following command.
virtualenv ~/virtualenvironment/my_new_app

You can add –no-site-packages in the command if you want to isolate your environment from the main site-packages directory.

  • To begin working on your project, you have to activate the virtual environment after creating it. For this, cd into your project directory’s bin folder.
cd ~/virtualenvironment/my_new_app/bin
  • Finally, activate your environment using the following command.
source activate

After execution of the above command, python virtualenv will be activated on your machine. Notice how the prompt of your shell changed to show the active environment. That is how you can see that you’re in your new environment.

After activating the virtual environment, any new installations will be installed to this environment only. The installations will have no effect on the python installation in the /etc/bin folder of the system.

To deactivate your python virtualenv, just type “deactivate” in the command line.

What Does Virtualenv do?

Packages installed in the virtual environment will not affect the global Python installation. Virtualenv does not create every file needed to get a whole new python environment. It uses links to global environment files instead in order to save disk space end speed up your virtualenv. Therefore, there must already have an active python environment installed on your system.

Install a Package in your Virtualenv

If you look at the bin directory in your virtualenv, you’ll see easy_install which
has been modified to put eggs and packages in the virtualenv’s site-packages
directory.

To install an app in your Virtualenv, you can use the PIP package installer. For instance, you can use the following command to install flask in the virtual environment.

pip install flask

If you are using python3, you can use pip3 instead of pip in the above command.

You don’t have to use sudo since the files will all be installed in the virtualenv directory which was created as your own user account.

Conclusion

In this article, we have discussed how to create and use virtual environments using the python virtualenv module. To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on the port scanner in python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

For further reading, please see the following documentation:
http://flask.pocoo.org/docs/installation/#virtualenv
http://pypi.python.org/pypi/virtualenv

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us