In this document, you will find step-by-step instructions to set up a VS Code virtual environment with the latest Python version.
Installing VS Code
Go to the VS Code download page and install VS Code (https://code.visualstudio.com/download) Accept the license agreement Check the following:
Installing Python
Go to the Python download page and download the latest version of Python (https://www.python.org/downloads/) Do not forget to check the ADD to PATH box!!!!
Setting up VS Code
Create or choose a folder on your computer where you will have the Python files. On the left-hand side click the extensions button from the menu
Install the following extensions: - Python Extension Pack extension - Better comments extension
After you install the extensions, you should see a Python logo appear in the VS Code menu. This is the Python Environment manager extension, which will help us manage your virtual environments. Do not worry if you do not have the power shell and GitHub options available (these can be obtained by installing additional extensions but are unnecessary for this workshop). Click the Explorer (two stacked papers) in the VS Code menu and open your Python programming folder
Create a new workspace, if you donât have one, by selecting âSave Workspace As âŚâ from the âFileâ drop-down menu. Then name your workspace and save it.
Right-click anywhere in the sub-menu and select âAdd a folder to workspaceâ.
The selected folder will appear in your workspace. In my case, I have created a new âRedPitaya_workshopâ folder. Please focus only on this directory. The other directories represent the case where you already have a few projects in your VS Code workspace.
Choose the Python interpreter in the blue-line menu at the bottom of VS Code. We will change the interpreter later when we create a virtual environment.
Creating a virtual environment (skip this step if time-pressured ) (################################################)
Right-click on the newly created folder and click âCopy Path (Shift+Alt+C)â.
Open a new terminal in VS Code
Move to the selected folder using the cd command; Type cd and then right-click (the path you copied should automatically paste itself). Then press enter.
Type in python -m venv <virtual environment name>. In my case, Iâm creating a new virtual environment called âworkshopâ.
As soon as you hit enter, you should see a bunch of new folders appear in your workspace.
We have just created a new virtual environment inside our selected folder. Activating the environment Now we need to activate the virtual environment Click on the Python logo on the left-hand menu in VS Code. Refresh the existing environments.
As you can see, a new Virtual environment appeared under âVenvâ.
Click on the arrow that appears when you hover over the newly created virtual environment
The VS Code will offer you to select an interpreter. Click anywhere on the screen to ignore the option. If you look in the terminal, you should see something like this:
(################################################)
NOTE: You need to have the âRunning Scriptsâ option enabled on your computer. To fix this on Windows, go to âSettings > Update&Security > For developersâ and find the Power Shell section.
Check the box and click Apply. You should click Apply even though the box is already checked. Should you run into this issue with another OS, please look for a solution online.
Installing libraries
Type python into the terminal to see whether everything is OK.
You should see the python version displayed. - Exit Python by typing exit() or CTRL+Z - Now letâs install the Python libraries (https://packaging.python.org/en/latest/tutorials/installing-packages/) Please follow the instructions on the python webpage: - Ensure you can run pip from the command line
- Ensure âpipâ, âsetuptoolsâ, and âwheelâ are up to date - Use pip to install the following libraries (https://packaging.python.org/en/latest/tutorials/installing-packages/#id18) - pyvisa pyvisa-py - numpy - matplotlib - pandas - scipy Should you need to, you can deactivate the virtual environment by typing deactivate.
To reactivate it, go to Python environments, and click on the arrow beside the chosen environment.
Check if everything is OK
Finally, letâs check if everything is working as intended. Create a new python script by pressing CTRL+N, select python as the programming language, and import NumPy.
Save the file and wait for a couple of seconds.
If NumPy is underlined, that means the libraries are not installed in the current Virtual Environment. Please recheck the instructions above.
Running Python files
Press the arrow in the upper right corner to run your scripts. Finally, print hello world and see whether it displays in the terminal.