How to launch python Idle from a virtual environment (virtualenv)

PythonVirtualenvPython Idle

Python Problem Overview

I have a package that I installed from a virtual environment. If I just launch the python interpreter, that package can be imported just fine. However, if I launch Idle, that package cannot be imported (since it's only available in one particular virtualenv and not global). How can I launch Idle from a virtualenv, so that all packages from the virtualenv would be available?

Python Solutions

Solution 1 - Python

Short answer

  1. Start the virtual environment
  2. Run python -m idlelib.idle

From [this answer][other-answer].

Long answer

This answer assumes Python 3.

There are a few different virtual environment managers, each of which has a slightly different way of handling where python is installed and how it's run, [as detailed in this answer][envs].

This answer assumes the [venv][venv] module is used, and that it was [installed following the docs][venv-tutorial].

Note: Some Linux distributions package the venv module into a separate package: [Ubuntu][ubuntu-venv] and [Debian][debian-venv]

If the virtual environment was installed in a folder called my_project-venv by running python -m venv my_project-venv from inside the folder my_project, the virtual environment will be inside a new folder created by the module:

      ┝━ my_project-venv

On Windows, with Python 3.7.1, the files inside the my_project-venv folder will probably look like this:

      ┝━ Include
      ┝━ Lib
      ┝━ Scripts
      │     ┝━ ...
      │     ┝━ activate.bat
      │     ┝━ Activate.ps1
      │     ┝━ deactivate.bat
      │     ┕━ ...
      ┕━ pyvenv.cfg

The virtual environment can be started by running either the activate.bat or Activate.ps1 script, depending on whether [cmd or PowerShell is used][windows-shells]:

:: Using cmd.exe
cd my_project_dir
# Using PowerShell
cd my_project_dir

Note: These scripts don't keep the shell open if run by double-clicking them. Start a shell, then run them by typing the above commands, with the folder names changed for your project

On most other operating systems, the virtual environment folder will look like this:

      ┝━ bin
      │     ┝━ ...
      │     ┝━ activate
      │     ┝━ activate.csh
      │     ┝━
      │     ┕━ ...
      ┝━ include
      ┝━ lib
      ┝━ lib64
      ┕━ pyvenv.cfg

Then, from any shell other than csh or [fish][], activate the environment by:

# Most operating systems
cd my_project_dir
. my_project-venv/bin/activate

For csh and fish there are shell-specific scripts for activating the virtual environment (activate.csh and, respectively) and they can be run like the activate script.

Once the virtual environment has been activated on all operating systems, running the following will start IDLE with access to the packages installed into the virtual environment:

python -m idlelib.idle

[other-answer]: <> "Someone else's answer" [envs]: <> "StackOverflow answer explaining different virtual environment managers" [venv]: <> "venv module reference" [venv-tutorial]: <> "tutorial on using the venv module" [ubuntu-venv]: <> "Search Ubuntu repositories for venv module package" [debian-venv]: <> "search debian repositories for venv module package" [windows-shells]: <> "Microsoft docs introducing shells"

[fish]: <> "fish shell homepage"

Solution 2 - Python

For Python 3.6+, please see Paul Wicking's answer below.

In Python prior to 3.6, IDLE is essentially

from idlelib.PyShell import main
if __name__ == '__main__':

So you can launch it yourself unless you built the virtualenv without default packages.

Solution 3 - Python

Python 3.6 modernized and refactored idlelib. This change included the renaming of several methods. Because of this, idlelib.PyShell must now be accessed with idlelib.pyshell. The following snippet is based on the accepted answer and should work for any Python version:

#!/usr/bin/env python
"""Simple script to run Idle from a venv in PyCharm."""

    # Import for Python pre 3.6
    from idlelib.PyShell import main
except ModuleNotFoundError:
    # Import for Python version 3.6 and later
    from idlelib.pyshell import main

if __name__ == '__main__':

Solution 4 - Python

On Windows, a Python script run from command line like this might be run by other Python interpreter than the one used when using python command (it depends on py files association). If one wants to avoid this problem it's best to create a batch file idle.bat with the content python -c "from idlelib.PyShell import main; main()" and place it in the Scripts folder in the virtualenv. Also, like others noted idle needs both tcl and tk folders to work. The simplest solution is to create symbolic links from virtualenv to the base Python installation like this

(2.7) c:\python\virtualenv\2.7\Lib>mklink /d tcl8.5 "c:\Program Files\Python\2.7\tcl\tcl8.5"
symbolic link created for tcl8.5 <<===>> c:\Program Files\Python\2.7\tcl\tcl8.5
(2.7) c:\python\virtualenv\2.7\Lib>mklink /d tk8.5 "c:\Program Files\Python\2.7\tcl\tk8.5"
symbolic link created for tk8.5 <<===>> c:\Program Files\Python\2.7\tcl\tk8.5

Solution 5 - Python

I am using Ubuntu 15.04 operating system. I have installed some packages using virtualenv.

So, to run the files inside virtualenv including those packages I use the following commands in terminal

(Name of my virtual environment is venv):

#Activate the virtualenv venv
source venv/bin/activate

#To Run IDLE in virtualenv venv
python -m idlelib

After running the IDLE, you can open file using ctrl+o keyboard shortcut.

Solution 6 - Python

Putting a few answers together and here is how I do this on Window with a fully functional batch file.

Make idle.bat in your virtualenv's Scripts directory. It will create (unless they exist) both links to tcl and tk (version 8.5 as of writing) and put them in you virtualenv's Lib directory then it fires up idle. Copy and paste this code exactly into an editor. Change the path names for your current virtualenv and Python install (mine is the standard for 2.7) then save it into Scripts/idle.bat.

IF EXIST C:\<path to current virtualenv>\Lib\tcl8.5 (
REM do nothing
) ELSE (
	mklink /d C:\<path to current virtualenv>\Lib\tcl8.5 "c:\Python27\tcl\tcl8.5"
IF EXIST C:\<path to current virtualenv>\Lib\tk8.5 (
REM do nothing
) ELSE (
	mklink /d C:\<path to current virtualenv>\Lib\tk8.5 "c:\Python27\tcl\tk8.5"

python -c "from idlelib.PyShell import main; main()"

Run the script with Powershell (RUN AS ADMIN!) to open idle.

cd c:\<path to current virtualenv>\

Solution 7 - Python

@biomed I am on Windows and I was trying this. In my python2.6 folder I had to copy the python26/tcl/tcl8.5 and python/tcl/tk8.5 folders to python26/Lib and then I created the script above in my virtualenv's scripts folder. Worked great.

Solution 8 - Python

For me launching something like this just works (Linux terminal):

source venv/bin/activate && python `which idle` &

(venv is path to your venv obviously)

Solution 9 - Python

On Windows:

python -m idlelib


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin Le - KhnleView Question on Stackoverflow
Solution 1 - PythonMatthew WillcocksonView Answer on Stackoverflow
Solution 2 - Python9000View Answer on Stackoverflow
Solution 3 - PythonPaul WickingView Answer on Stackoverflow
Solution 4 - PythonPiotr DobrogostView Answer on Stackoverflow
Solution 5 - PythonarshoView Answer on Stackoverflow
Solution 6 - PythonsrockView Answer on Stackoverflow
Solution 7 - PythonTimView Answer on Stackoverflow
Solution 8 - PythonyanView Answer on Stackoverflow
Solution 9 - PythonffiView Answer on Stackoverflow