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
How to Add Folder and App Shortcuts to The Windows 11 Start Menu
US adds 5.6 GW of utility-scale clean energy in Q1, up 28% year-over-year: ACP
Creepshotorg
Umbc Baseball Camp
Pixel Speedrun Unblocked 76
Metra Union Pacific West Schedule
Stretchmark Camouflage Highland Park
Botanist Workbench Rs3
Melfme
The Haunted Drury Hotels of San Antonio’s Riverwalk
Lantana Blocc Compton Crips
Regal Stone Pokemon Gaia
Insidekp.kp.org Hrconnect
Craigslist Pets Athens Ohio
Nalley Tartar Sauce
Chic Lash Boutique Highland Village
Yakimacraigslist
Sni 35 Wiring Diagram
Crawlers List Chicago
Pocono Recird Obits
Wkow Weather Radar
Koninklijk Theater Tuschinski
Hefkervelt Blog
Synergy Grand Rapids Public Schools
Egusd Lunch Menu
Scott Surratt Salary
HP PARTSURFER - spare part search portal
How Much Is An Alignment At Costco
Bursar.okstate.edu
Fedex Walgreens Pickup Times
Att U Verse Outage Map
P3P Orthrus With Dodge Slash
Newcardapply Com 21961
Timothy Kremchek Net Worth
Boggle BrainBusters: Find 7 States | BOOMER Magazine
Dr. John Mathews Jr., MD – Fairfax, VA | Internal Medicine on Doximity
Rochester Ny Missed Connections
Toth Boer Goats
Pepsi Collaboration
Kornerstone Funeral Tulia
Top 25 E-Commerce Companies Using FedEx
Emily Browning Fansite
Weather In Allentown-Bethlehem-Easton Metropolitan Area 10 Days
Pathfinder Wrath Of The Righteous Tiefling Traitor
Trending mods at Kenshi Nexus
Europa Universalis 4: Army Composition Guide
Nurses May Be Entitled to Overtime Despite Yearly Salary
Shiftselect Carolinas
Verilife Williamsport Reviews
How Did Natalie Earnheart Lose Weight
Ranking 134 college football teams after Week 1, from Georgia to Temple
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.