Advertisements

How to associate a Python 3 virtual environment with Thonny

When you are developing a Python 3 application with Thonny, you may want to get Thonny to use a virtual environment to run and debug your application.

With this in mind, this post shows you how to associate a Python 3 virtual environment with Thonny.

Creating a virtual environment with python3-venv

This tutorial was created with Thonny running on Raspbian Stretch. The steps to associate a Python 3 virtual environment with Thonny running on Windows should be similar.

Before proceeding further, make sure that you had created a virtual environment. In case you need it, you can follow one of the following tutorials to create your Python 3 virtual environment:

For the purpose of this post, let's assume that we had created a Python 3 virtual environment at /home/pi/my-venv.

Steps to associate a Python 3 virtual environment with Thonny

Firstly, open up your Thonny IDE.

Thonny IDE opened for the first time on Raspbian Stretch 20181113

If you see that your Thonny IDE does not have the menu bar, click on Switch to regular mode on the top right corner. After you had done so, Thonny will show you a confirmation message:

Thonny on Raspbian Stretch 20181113 showing confirmation message after switching to regular mode

Click on Ok and reopen Thonny. After Thonny had been reopened, it will show a menu bar at the top:

Thonny on Raspbian Stretch 20181113 with menu bar

Given that, go to Tools -> Options:

Thonny on Raspbian Stretch 20181113 showing first tab of Options

Click on the Interpreter tab:

Thonny on Raspbian Stretch 20181113 Interpreter tab showing configuration as using the same Python interpreter which runs Thonny

As shown above, Thonny is using the same Python interpreter which runs Thonny to run your Python 3 application. Click on the combo box and choose Alternative Python 3 interpreter or virtual environment:

Thonny on Raspbian Stretch 20181113 Interpreter tab showing configuration screen for Alternative Python 3 interpreter or virtual environment

Click on Locate another python executable... and a file dialog will appear for you to choose the python executable inside your virtual environment:

Thonny on Raspbian Stretch 20181113 showing Open file dialog

Click Ok to confirm your selection:

Thonny on Raspbian Stretch 20181113 showing that my-venv python executable being used as the Interpreter for running Python 3 application

Click Ok again to confirm your configuration changes. Once you had done so, you will see that Thonny restarts the shell below with the Python executable from your virtual environment:

Thonny on Raspbian Stretch 20181113 showing that it had restarted its shell to use the Python executable in my-venv virtual environment

Given that, Thonny will run your Python 3 application inside your virtual environment.

How to associate a Python 3 virtual environment with Thonny

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap