Setting Up Virtual Environments in Python | Information Technology and Computing Support (2024)

Setting Up Virtual Environments in Python

The Python Virtual Environments system is a tool to make sure that packages related to different projects are stored in different places so that they do not conflict. A Python Virtual Environment isisolated from your other python projects or environmentsand allows for that environment to have its own dependencies to remain independent.

WINDOWS

Getting Started

  1. Install Python
    1. If you are on an ENGINEERING Windows Desktop, install Python 2.7 through software center
  2. Add Python to PATH
    1. Navigate: Control Panel -> User Accounts -> Change my Environment Variables
    2. Inside the top window labeled 'User Variables' select 'Path' and edit it
    3. Select 'Browse' and navigate to C:\python27 and select 'ok'
    4. Select 'Move Down' and repeat step 3 for C:\python27\Scripts
  3. Open a new CMDprompt (Windows Key + R, cmd.exe)
  4. Install virtualenvthrough the command pip install virtualenv. If you are not an administrator do not update if prompted
  5. Check that virtualenv is installed through the command pip --version
  6. Install virtualenvwrapper-win through the command pip install virtualenvwrapper-win

Test Functionality

  • Create a test environment with mkvirtualenvexample, and this will make a folder called Envsin your user directory
  • Change your python directories to a project folder in command prompt and run setprojectdirto set that as your development folder.
  • To leave an environment use deactivate
  • You can enter an environment by using workonexampleand from there can invoke package installations like pip install flask
  • To completely delete an environment make sure you leave the environment first and use rmvirtualenv exampleto delete the environment

LINUX

Getting Started

Virtual Environments on Python 2.7

  • Create a virtual environment in your current directory for a project with the command:virtualenv my_project
    • "my_project" is whatever name you would like to give this environment
  • To create a virtual environment with a specific version of python use the command:virtualenv -p /usr/bin/python2.7 my_project
    • "python2.7" can be replaced with the version you would like to use

Virtual Environments on Python 3.5+

  • Create a virtual environment in your current directory for a project with the command:python3-m venvmy_project
    • "my_project" is whatever name you would like to give this environment

Using the Virtual Environment

Bash shell:

  • Start the virtual environment by activating with the command:source my_project/bin/activate

csh/tcsh:

  • Start the virtual environment by activating with the command:source my_project/bin/activate.csh

Pip can now be used as usual for package installs

  • When you run 'python' it will be the version specified in Step 1
  • When you run 'pip', you're running the correct version
  • All modules are installed inside that environment

When you are finished working in the environment, exit using the command:deactivate

For more details visit: http://docs.python-guide.org/en/latest/dev/virtualenvs/

or download the PDF: python-guide.pdf

Setting Up Virtual Environments in Python | Information Technology and Computing Support (2024)
Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6483

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.