In this small blog post I explain how you can work with a virtual environment in Thonny on a Raspberry Pi. But first, let’s create a virtual environment using venv.
- Open the terminal
- Change directory to the folder of your project (cd), or create a new one (mkdir)
- Inside that folder, create the virtual environment using python3 -m venv ./env
You can activate the virtual environment inside your terminal using the command source ./env/bin/activate. However, if you would like to associate Thonny, the default Python editor on Raspbian, with your virtual environment, these are the steps you should take.
Open Thonny, and make sure you switch to Regular Mode. This will allow you to access the menu.
Open Tools > Options and select the Interpreter tab. In the combo box, select an Alternative Python 3 interpreter or virtual environment.
Browse yo the folder of your virtual environment. Inside the bin folder, you should be able to find the python3 executable. Select it, close the menu. Now Thonny will restart the shell and start Python from inside the virtual environment.
Great success!
super, merci de ton aide.
Thanks a lot! Worked like a charm!
Just one thing: can you set Thonny to run this virtual environment as default?
dN4MVFnD5KylUX888jzPds1zRK78KDg7MPK85mWqSiWp44FLIwx5pvGcPRMU23RlebOFyGWXmPHQLFo2eWFXP7ulpoIMVl9l