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.