0% found this document useful (0 votes)
33 views

Python Virtual Environment

This document provides steps to create a virtual environment in Visual Studio Code for Python development on macOS. It explains how to create a virtual environment called 'web_scrape_env', a project folder called 'web_scrape', and a test file called 'app.py'. It also describes installing packages like Beautiful Soup and Requests in the virtual environment.

Uploaded by

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

Python Virtual Environment

This document provides steps to create a virtual environment in Visual Studio Code for Python development on macOS. It explains how to create a virtual environment called 'web_scrape_env', a project folder called 'web_scrape', and a test file called 'app.py'. It also describes installing packages like Beautiful Soup and Requests in the virtual environment.

Uploaded by

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

Creating virtual environment in Visual Studio Code

for Python on macOS

Python™ is a registered trademark of the PSF. Giovanni Dalla Vecchia - 20/06/2020


Microsoft™ Visual Studio Code is a registered trademark of Microsoft Corporation VSCode_Python_Venv - v1
Will assume you have the following knowledge and your machine the following
softwares:

• Visual Studio Code (VSCode) with <python> and <doraemon-runner> extensions

• Python3

• Confident to use VS Code regarding folder (create/open), terminal (open/close),


files (create/open/edit), install dependencies (when required), configuration
(command palette)

• Know how to create folders and navigate them via terminal

• Know how to run a Python application <python3 “file_name.py>

Python™ is a registered trademark of the PSF. Giovanni Dalla Vecchia - 20/06/2020


Microsoft™ Visual Studio Code is a registered trademark of Microsoft Corporation VSCode_Python_Venv - v1
Create a virtual environment to develop a Python application using VSCode

• Environment name: ‘web_scrape_env’


• Project folder: ‘web_scrape’

• Project file for test the environment: ‘app.py’


• Packages to install: ‘BeautifulSoup4’ and ‘Requests’

Python™ is a registered trademark of the PSF. Giovanni Dalla Vecchia - 20/06/2020


Microsoft™ Visual Studio Code is a registered trademark of Microsoft Corporation VSCode_Python_Venv - v1
▶︎ in VSCode terminal ( ⌃` ) create the virtual environment <python3 -m venv web_scrape_env>

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ create the folder ‘web_scrape <mkdir web_scrape>

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
1 2

▶︎ both folders have been created (1)’web_scrape’ and (2)’web_scrape_env’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ open ( ⌘O ) the folder project ‘web_scrape’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ create ( ⌘N ) a new file ‘app.py’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ open ‘Workspace Settings (JSON)’ on Command Palette ( ⇧⌘P )

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ type between curly brackets “python-pythonPath”: “python”

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ open the terminal ( ⌃` ), change to the environment folder then copy its path

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ replace ‘python’ to the path of the virtual environment ‘web_scrape_env’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ if you get a message to install ‘Linter’ just confirm. Linter is a tool to analyse the source code

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ now your project is pointing to the virtual environment ‘web_scrape_env’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
What is that mean ‘point to the Virtual Environment web_scrape_env’?

• The project will have access to the packages inside the environment

• There is no need to install packages globally for all your projects

• Imagine if you have an application that uses a package version 1.0.1 with a
specific function/method, but the other version let is say 2.3.5 works
completely different. You have two options: change all your code or avoid any
upgrade to the new version. But if you have a version 1.0.1 in your
environment v1.0.1 you do not need to worry, unless you install the new
version 2.3.5.

• DO NOT forget to change your virtual environment when create a new


application.
Giovanni Dalla Vecchia - 20/06/2020
VSCode_Python_Venv - v1
▶︎ enter in the terminal the command ‘pip list’. That lists the packages installed in the virtual environment

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ location of all the packages available on your environment ‘web_scrap_env’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
1
2

▶︎ on your project (1) activate virtual environment and (2) install the packages ‘beautiful soup (bs4)’ and ‘requests’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ now both packages are available to be used on your project

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
1

▶︎ type the code (1) above inside the ‘app.py’ then (2) run ‘python app.py’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ if all goes well you should see the following return on your prompt

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
▶︎ if you need to uninstall the packages run the command ‘pip uninstall bs4 requests’

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1
Thanks very much

Giovanni Dalla Vecchia - 20/06/2020


VSCode_Python_Venv - v1

You might also like