Intro to Backend Development
  • Introduction
  • SP25 Syllabus
  • Apply to Take the Course
  • Getting Started
  • Weekly Feedback Form
  • Ed Discussion
  • Intro to Backend SP25 Google Calendar
  • Cheat Sheets
    • Assignment Requirements
    • Assignment FAQs
    • Error FAQs 😢
    • Concept FAQs
    • Postman
    • Command Line
    • Virtual Environment
  • Chapters
    • 1. Routes
      • Pre-Class TODO's
      • Lecture
      • Assignment Handout
      • API Specification
    • 2. Databases
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 3. Relational Databases
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 4. Abstractions
      • Pre-Class TODO's
      • Lecture
      • Demo
      • Assignment Handout
      • API Specification
    • 5. Containerization
      • Pre-Class TODO's
      • Docker Installation
      • Lecture
      • Demo
      • Assignment Handout
    • 6. Deployment
      • Lecture
      • Demo
      • Assignment Handout
    • 7. Images
      • Demo
      • Assignment Handout
    • 8. Authentication
      • Lecture
      • Demo
      • Assignment Handout
    • (Work in Progress) OAuth
      • Pre-Class TODO's
      • Lecture
      • Demo
      • OAuth 2.0 vs OpenID
      • Flask / OpenID example
  • Additional Topics
    • Git and Github
    • HackOurCampus
  • Other AppDev Courses
    • Intro to iOS Development
    • Intro to Android Development
    • Intro to Digital Product Design
  • Deprecated
    • Previous Semester Syllabi
      • FA22 Syllabus
      • SP22 Syllabus
      • FA21 Syllabus
      • SP21 Syllabus
      • FA20 Syllabus
      • SP20 Syllabus
    • Deployment Pre-Class TODO's
    • PA6 Assignment Handout
    • Deployment Demo
    • Final Project (Spring 2019)
      • Final Project Award Winners
Powered by GitBook
On this page
  • 1. Install virtualenv
  • 2. Create a venv folder
  • 3. Activate the venv folder
  • 4. Install requirements.txt
  • 5. Run your code!
  • 6. If you're done, deactivate
  • Common Errors
  • no such file or directory: requirements.txt

Was this helpful?

  1. Cheat Sheets

Virtual Environment

Contributors: Alanna Zhou, Shungo Najima

PreviousCommand LineNext1. Routes

Last updated 1 year ago

Was this helpful?

You can consult some for more information!

In the following code blocks, the dollar sign $ just means you should be typing what follows in your terminal/command prompt!

1. Install virtualenv

Navigate to the directory that you want to be coding in, and make sure you have virtualenvinstalled

$ pip install virtualenv

2. Create a venv folder

You could actually name this anything you want, but it's good practice to call it venv

You create this folder so you can activate later to essentially "enter your virtual environment"

$ python3 -m venv <name of virtual environment>

3. Activate the venv folder

You can see if it's been created with ls for MacOS or dir for Windows

MacOS command to activate virtual environment:

$ . venv/bin/activate

Windows command to activate virtual environment:

$ .\venv\Scripts\Activate.ps1

If you are getting an error that reads: "cannot be loaded because the execution of scripts is disabled on this system"., close PowerShell (if open), look up PowerShell on Windows search, click on "run as administrator", then run the command below:

$ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

If you're still getting an error, try running these commands on PowerShell instead of Command Prompt.

4. Install requirements.txt

Now that you're in the environment, your terminal should have a (venv) next to your cursor, so you can now install the requirements to setup your virtual environment

(venv) $ pip3 install -r requirements.txt

Make sure requirements.txt exists where you are running this command! (again, you can check this using ls/dir to list everything in your current directory).

If you are curious, -r is a flag that installs the requirements recursively to install all requirements defined in requirements.txt.3

5. Run your code!

Now you can run code with the dependencies that were installed by Step 4.

Here's just an example:

(venv) $ python3 <YOUR FILE NAME>.py 

6. If you're done, deactivate

If you're done developing for your project for the time being, or if you want to work on another project that has another virtual environment, you can deactivate the one that you're currently in

(venv) $ deactivate
$ 

What commands to use and when to use them

  • Steps #1, 2, and 4 are only done once for every new project you'll be creating a virtual environment for.

  • Steps #3, 5, and 6 are what you'll be using every time after that to use your virtual environment!

Common Errors

no such file or directory: requirements.txt

There are two issues that cause this problem:

  1. You are not in the directory where requirements.txt exists. You'll know this is the case if enter ls (MacOS) or dir (Windows) into your terminal and you don't see requirements.txt or if you didn't spell it right in your command.

If you are curious as to what exactly this is doing to your computer, it is allowing the current user to run scripts. RemoteSigned is one of many execution policies which you can read about , and CurrentUser is one of many scopes which you can read about .

You are trying to do pip3 install -r requirements.txt inside of a correctly created and activated venv, but your venv is nested within a path that contains a folder that has a space in the name. Here's a good that explains the issue.

helpful Python docs
here
here
Stack Overflow post