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

9

76

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?

Lacerated answered 7/2, 2011 at 17:1 Comment(0)
P
42

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__':
  main()

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

Pieper answered 7/2, 2011 at 18:34 Comment(10)
From which folder would I run those script? ThanksLacerated
@Khnle: from the folder where your project resides, aparently, after you have activated the virtualenv. I just tried it, and it does work. Put this script to your virtualenv's bin.Pieper
I created an idle script in the bin/ of my virtualenv and I used the virtualenv python in my shebang but it complains about not being able to find Tkinter. I checked and Tkinter can be imported in the regular python2.7.1 but I can't import Tkinter from my virtualenv python. How can I make Tkinter available to my virtualenv python ( I used no site-packages for an other reason)Wayside
@biomed: look at your virtualenv's lib directory. locate any .pth file in it; it's easy to see how it works. the name is an importable name, the content is a path. try pulling such a trick with Tkinter. (no, I did not try it with Tkinter.)Pieper
Inside an active virtualenv you can just type python -c "from idlelib.PyShell import main; main()" and this saves you from keeping a script somewhere.Cleres
indeed, python -m idlelibWaverly
python -m idlelib doesn't work for me. Can someone explain what is supposed to happen here? It just can't find the module idlelib. What are the paths your using?Culprit
Just tried this with default virtualenv under Python 3.6 and it did not work using import in a script. @MatthewWillcockson 's python -m idlelib.idle does work.Refutative
For Python 3.6.8, use python3 -m idlelib or python3 -m idlelib.idle.Resort
See Paul Wicking's answer below for a script which will work for both pre 3.6 Python and after. That one did the trick for me.Othilie
C
110

Short answer

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

From this 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.

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

Note: Some Linux distributions package the venv module into a separate package: Ubuntu and Debian

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_dir
      │
      ┝━ my_project-venv

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

my_project-venv
      │
      ┝━ 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:

:: Using cmd.exe
cd my_project_dir
.\my_project-venv\Scripts\activate.bat
# Using PowerShell
cd my_project_dir
.\my_project-venv\Scripts\Activate.ps1

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:

my_project-venv
      │
      ┝━ bin
      │     ┝━ ...
      │     ┝━ activate
      │     ┝━ activate.csh
      │     ┝━ activate.fish
      │     ┕━ ...
      │
      ┝━ 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 activate.fish, 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
Cube answered 29/6, 2016 at 16:5 Comment(4)
Pro-tip: set this as an alias using alias idle='python -m idlelib.idle' You can also do this on windows by setting DOSKEY: https://mcmap.net/q/73583/-aliases-in-windows-command-promptStumpf
for most linux distros, that'll be python3 -m idlelib.idleSoonsooner
Why the .idle at the end? I get it started by simply : python -m idlelib.Resort
@Resort Great question. Up until now, my only reason was that the original source had it. Looking at the module's source, the .idle runs some extra code that's useful when developing IDLE. I don't think it does any harm? 🤷‍♂️Cube
P
42

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__':
  main()

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

Pieper answered 7/2, 2011 at 18:34 Comment(10)
From which folder would I run those script? ThanksLacerated
@Khnle: from the folder where your project resides, aparently, after you have activated the virtualenv. I just tried it, and it does work. Put this script to your virtualenv's bin.Pieper
I created an idle script in the bin/ of my virtualenv and I used the virtualenv python in my shebang but it complains about not being able to find Tkinter. I checked and Tkinter can be imported in the regular python2.7.1 but I can't import Tkinter from my virtualenv python. How can I make Tkinter available to my virtualenv python ( I used no site-packages for an other reason)Wayside
@biomed: look at your virtualenv's lib directory. locate any .pth file in it; it's easy to see how it works. the name is an importable name, the content is a path. try pulling such a trick with Tkinter. (no, I did not try it with Tkinter.)Pieper
Inside an active virtualenv you can just type python -c "from idlelib.PyShell import main; main()" and this saves you from keeping a script somewhere.Cleres
indeed, python -m idlelibWaverly
python -m idlelib doesn't work for me. Can someone explain what is supposed to happen here? It just can't find the module idlelib. What are the paths your using?Culprit
Just tried this with default virtualenv under Python 3.6 and it did not work using import in a script. @MatthewWillcockson 's python -m idlelib.idle does work.Refutative
For Python 3.6.8, use python3 -m idlelib or python3 -m idlelib.idle.Resort
See Paul Wicking's answer below for a script which will work for both pre 3.6 Python and after. That one did the trick for me.Othilie
D
11

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."""

try:
    # 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__':
    main()
Devonadevondra answered 9/2, 2018 at 18:15 Comment(0)
C
9

On Windows, a Python script run from command line like this some_script.py might be run by other Python interpreter than the one used when using python some_script.py 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
Cardiomegaly answered 28/4, 2012 at 19:50 Comment(3)
+1, thank you for this. Just a note: DON'T install Python under Program Files because of the space in the pathname! Virtualenv was failing for me, and re-installing Python under C:\ (default) fixed it.Autacoid
I could not get the idle.bat file to work on my Python 2.7 system, even after creating the symbolic folder links. I initially created them under Lib, and idle.bat launch failed. Then I created a folder called tcl under the virtual environment, and created the symbolic folder links there, and it still produced an error:Traceback (most recent call last): "... _tkinter.TclError: Can't find a usable init.tcl in the following directories: {C:\Users\AMS\my_env\tcl\tcl8.5} C:/Python27/lib/tcl8.5 C:/Users/AMS/my_env/lib/tcl8.5 ... This probably means that Tcl wasn't installed properly." Any ideas?Autacoid
OK, copying the two folders as Tim has pointed out below (instead of symbolic links) fixed the problem, and I can launch IDLE through idle.bat.Autacoid
G
6

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.

Gus answered 20/7, 2016 at 14:40 Comment(0)
T
3

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>\
./Scripts/idle.bat
Trilobite answered 10/9, 2014 at 19:31 Comment(1)
Thanks! I was looking for a script exactly like this I had made earlier on a different machine.Stonemason
T
2

@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.

Tavish answered 5/5, 2011 at 14:28 Comment(0)
H
2

On Windows:

C:\foo\bar\{venv_name}\activate
python -m idlelib
Harber answered 20/8, 2021 at 10:6 Comment(0)
H
1

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

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

(venv is path to your venv obviously)

Hydatid answered 14/9, 2015 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.