Posit Workbench User Guide - Python Environments in VS Code (2024)

When using VS Code for Python development, we recommend installing the Python extension. The Python extension enables many features that will be useful for Python developers, including support for managing and using virtual environments. This page focuses on VS Code-specific features for working with Python virtual environments.

Selecting an interpreter

VS Code will automatically detect when you are in a directory that contains Python files. You will know that VS Code has correctly detected a Python project when you see a Python interpreter appear in the bottom right corner.

Posit Workbench User Guide - Python Environments in VS Code (1)

To change the selected interpreter open the Command Palette using Cmd + Shift + P on Mac or Ctrl + Shift + P on Windows. Then type “Python: Select Interpreter”. Alternatively, you can click on the current interpreter in the bottom right corner.

VS Code will automatically detect available interpreters and virtual environments. If VS Code does not automatically detect your desired interpreter, you can manually specify the path.

Posit Workbench User Guide - Python Environments in VS Code (2)

Now that you have selected an interpreter, VS Code will:

  • Use this interpreter to execute Python code when you press the play button in the top right corner.
  • Automatically activate this interpreter when you open a new terminal.
  • Use this interpreter for Notebooks and Interactive Windows.

Project workflow

We recommend using a unique virtual environment for each project. This section walks you through the typical development flow of using virtual environments in VS Code.

Create a new project

Open a new VS Code Session in Workbench. Then create a new folder that will serve as the root directory of your project. You can do this using the terminal. Open the terminal by launching the Command Palette and then typing “Terminal: Create New Terminal”.

mkdir ~/my-projecttouch ~/my-project/hello-world.pyecho "print('hello world')" > ~/my-project/hello-world.py

Then re-open VS Code inside the new project folder. Open the Command Palette and type “File: Open Folder…”. Then enter the path to the directory you just created “~/my-project”.

Posit Workbench User Guide - Python Environments in VS Code (3)

Create a virtual environment

Use the Command Palette to open a new terminal session with “Terminal: Create New Terminal”. Then create a new virtual environment by running the following commands in the terminal:

python -m venv .venvsource .venv/bin/activatepython -m pip install --upgrade pip setuptools wheel

Tip

If you want to enable the Jupyter Notebook features in VS Code you should also install ipykernel in your virtual environment:

python -m pip install ipykernel

You can verify the python command will now use your virtual environment by running:

which python# /usr/home/<username>/my-project/.venv/bin/python

Tip

When you create a virtual environment using python -m venv .venv, VS Code will use the version of Python associated with the python command.

  • You can check what version by typing python --version in the terminal.
  • You can find the path to the interpreter by typing which python in the terminal.

If you want to use a specific version of Python, you can create the virtual environment using the absolute path to the interpreter. For example, you could create a new virtual environment using Python version 3.10.4 with the following commands:

/opt/python/3.10.4/bin/python -m venv .venvsource .venv/bin/activatepython -m pip install --upgrade pip setuptools wheel

This assumes that Python 3.10.4 has already been installed in the /opt/python/3.10.4 directory. We recommend that admins install all versions of Python in /opt/python. If you are unsure where Python has been installed on your server please reach out to your server administrator.

Set the default project interpreter

Now you can configure VS Code always to use this virtual environment when working on this project. Open the Command Palette and type “Python: Select Interpreter”. VS Code should automatically recommend the virtual environment you just created. Select it using the arrow keys and press enter. If VS Code does not automatically detect or recommend the new virtual environment, you can manually enter the path: “./venv/bin/python”.

Posit Workbench User Guide - Python Environments in VS Code (4)

Alternatively, you can select the default project interpreter by clicking on the Python section of the Status Bar located at the bottom right of the window.

Posit Workbench User Guide - Python Environments in VS Code (5)

Run your code

Once you have selected a default project Python interpreter, the virtual environment will be automatically activated and used whenever you open the project. Open a new terminal to see the changes take effect and watch VS Code automatically activate the virtual environment.

Posit Workbench User Guide - Python Environments in VS Code (6)

You can execute your Python code using the play button at the top right corner.

Posit Workbench User Guide - Python Environments in VS Code (7)

Alternatively, you can execute your Python code directly from the terminal.

python hello-world.py# hello world
Back to top
Posit Workbench User Guide - Python Environments in VS Code (2024)
Top Articles
Experian’s Document Upload Service - Experian
Flex Pay
Somboun Asian Market
Urist Mcenforcer
Ffxiv Shelfeye Reaver
Craftsman M230 Lawn Mower Oil Change
Wisconsin Women's Volleyball Team Leaked Pictures
Top Financial Advisors in the U.S.
Erskine Plus Portal
Corpse Bride Soap2Day
Optum Medicare Support
Pbr Wisconsin Baseball
13 The Musical Common Sense Media
Gt Transfer Equivalency
454 Cu In Liters
Turning the System On or Off
7 Low-Carb Foods That Fill You Up - Keto Tips
Pricelinerewardsvisa Com Activate
Indiana Wesleyan Transcripts
Kamzz Llc
FDA Approves Arcutis’ ZORYVE® (roflumilast) Topical Foam, 0.3% for the Treatment of Seborrheic Dermatitis in Individuals Aged 9 Years and Older - Arcutis Biotherapeutics
Finalize Teams Yahoo Fantasy Football
Japanese Mushrooms: 10 Popular Varieties and Simple Recipes - Japan Travel Guide MATCHA
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
At&T Outage Today 2022 Map
Jordan Poyer Wiki
kvoa.com | News 4 Tucson
Cornedbeefapproved
Sinai Sdn 2023
How Do Netspend Cards Work?
Kelley Fliehler Wikipedia
Otis Offender Michigan
Stolen Touches Neva Altaj Read Online Free
Www Craigslist Com Shreveport Louisiana
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
Arcadia Lesson Plan | Day 4: Crossword Puzzle | GradeSaver
Tds Wifi Outage
Hindilinks4U Bollywood Action Movies
Temu Y2K
Craigslist Tulsa Ok Farm And Garden
Cranston Sewer Tax
Barstool Sports Gif
412Doctors
Timothy Warren Cobb Obituary
Professors Helpers Abbreviation
Dontrell Nelson - 2016 - Football - University of Memphis Athletics
Copd Active Learning Template
Bonecrusher Upgrade Rs3
The 13 best home gym equipment and machines of 2023
Kidcheck Login
Arnold Swansinger Family
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5786

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.