How to run Python IDLE in a Pipenv created virtual environment

How to run Python IDLE in a Pipenv created virtual environment

If you've heard of Pipenv, it's a neat little utility that helps you create a virtual environment for your Python project and then you can install all the dependencies in there and it would not pollute your based python environment. This way you can maintain different versions of the library. For things like for quick scripts I like to use the default Python IDLE porgram that comes with Python on Windows because it really helps you create something quickly using the REPL environment which I haven't been able to figure out in VS Code because you have execute the code everytime.

So let's get into it, the first thing you want to do is open up a command prompt window (Ctrl+R to open run and then type 'cmd'). Let's say you want to make a project so you'll make a new directory for it (we'll use C:\tmp) in which you will have your Python project and then you'll execute pipenv install pandas. It's going to take a while for it to create the virtual environment and install the pandas library on it.

Open up notepad and create a notepad file where we're going to be typing some code that will open the IDLE program. This is where it's found on Stack Overflow and hopefully you're using Python 3.6 or later (3.8 is the current version at the time of this writing) because this code is only compatible with Python 3.6+ and type in this:

from idlelib.pyshell import main

if __name__ == '__main__':

We will run this piece of code in the virtual environment we just created, that will call to open up an IDLE window with the context/environment where the pandas library/dependency is already installed. Then do pipenv shell which opens up the shell environment for Pipenv within the virtual environment in which all of your libraries is installed where we can run Python as usual. Now execute python and a new IDLE window will open up.

In this REPL, if I do import pandas as pd you know it's imported. So now what I want to do is document the code in a new file that I will run every time. Opened up another IDLE window from within IDLE here and and save that as another file. Type in name = Shuaib' in that file and then hit F5 (Run the file). Now if you type name in the REPL, you'll see the variable was available.

That's how easy it is to run IDLE in a Pipenv created virtual environment and keep your global base python namespace clean!

Show Comments