Sign in

Python Virtual Environments Tutorial for Beginners

Introduction

Virtual Environments

$ sudo apt install python3.6-venv

Creating a Virtual Environment

$ mkdir yourproject
$ cd yourproject
import requests#Call the api 
response = requests.get('https://api.github.com')
#Print response code
print(response.status_code)
$ python -m venv example-env
yourproject/├── example-env│ ├── bin│ ├── include│ ├── lib│ ├── lib64│ └── pyvenv.cfg└── main.py
$ source example-env/bin/activate
$(example-env) user@ubuntu:
$ pip install requests
$ python main.py
$ deactivate

Creating and Using a Requirements File

$ pip freeze > requirements.txt
$ pip install -r requirements.txt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store