How to launch python Idle from a virtual environme

2019-01-08 09:20发布

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?

8条回答
贪生不怕死
2楼-- · 2019-01-08 10:08

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
查看更多
Bombasti
3楼-- · 2019-01-08 10:11

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

查看更多
登录 后发表回答