如何从虚拟环境 (virtualenv) 启动 python Idle
我有一个从虚拟环境安装的软件包。如果我只是启动 python 解释器,则可以很好地导入该包。但是,如果我启动 Idle,则无法导入该包(因为它仅在一个特定的 virtualenv 中可用,而不是在全局中可用)。如何从 virtualenv 启动 Idle,以便 virtualenv 中的所有包都可用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简短回答
python -midlelib.idle
从这个答案 。
长答案
这个答案假设Python 3。
有几种不同的虚拟环境管理器,每种管理器处理Python安装位置和运行方式的方式略有不同,如本答案所述。
此答案假设
venv
模块,并且已安装 遵循文档。注意:某些 Linux 发行版将 venv 模块打包到单独的包中: Ubuntu 和 Debian
如果通过运行
python -m venv my_project 将虚拟环境安装在名为
从文件夹my_project-venv
的文件夹中-venvmy_project
中,虚拟环境将位于模块创建的新文件夹中:在 Windows 上,使用 Python 3.7.1,
my_project 中的文件-venv
文件夹可能如下所示:可以通过运行
activate.bat
或Activate.ps1
脚本来启动虚拟环境,具体取决于是否< a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands#BKMK_OVR" rel="noreferrer" title="介绍 shell 的 Microsoft 文档">cmd 或 PowerShell:
注意:如果通过双击运行这些脚本,它们不会使 shell 保持打开状态。启动 shell,然后通过键入上述命令来运行它们,并为您的项目更改文件夹名称
在大多数其他操作系统上,虚拟环境文件夹将如下所示:
然后,从
之外的任何 shell csh
或 fish,通过以下方式激活环境:对于
csh< /code> 和
fish
有用于激活虚拟环境的特定于 shell 的脚本(分别为activate.csh
和activate.fish
),它们可以像activate
脚本一样运行。在所有操作系统上激活虚拟环境后,运行以下命令将启动 IDLE,并可以访问安装到虚拟环境中的软件包:
Short answer
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 runningpython -m venv my_project-venv
from inside the foldermy_project
, the virtual environment will be inside a new folder created by the module:On Windows, with Python 3.7.1, the files inside the
my_project-venv
folder will probably look like this:The virtual environment can be started by running either the
activate.bat
orActivate.ps1
script, depending on whethercmd
or PowerShell is used: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:
Then, from any shell other than
csh
or fish, activate the environment by:For
csh
andfish
there are shell-specific scripts for activating the virtual environment (activate.csh
andactivate.fish
, respectively) and they can be run like theactivate
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 3.6+,请参阅下面的 Paul Wicking 的回答。
在 3.6 之前的 Python 中,IDLE 本质上是
所以你可以自己启动它,除非你构建了没有默认包的 virtualenv。
For Python 3.6+, please see Paul Wicking's answer below.
In Python prior to 3.6, IDLE is essentially
So you can launch it yourself unless you built the virtualenv without default packages.
Python 3.6 现代化和重构
idlelib
。此更改包括对几种方法的重命名。因此,现在必须使用idlelib.pyshell
访问idlelib.PyShell
。以下代码片段基于已接受的答案,并且适用于任何 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 withidlelib.pyshell
. The following snippet is based on the accepted answer and should work for any Python version:在 Windows 上,像这样从命令行运行的 Python 脚本
some_script.py
可能会由其他 Python 解释器运行,而不是使用python some_script.py
命令时使用的解释器(这取决于关于 py 文件关联)。如果想避免此问题,最好创建一个批处理文件idle.bat
,其内容为python -c "fromidlelib.PyShell import main; main()"
和将其放置在 virtualenv 的 Scripts 文件夹中。此外,正如其他人指出的,idle 需要 tcl 和 tk 文件夹才能工作。最简单的解决方案是创建从 virtualenv 到 base Python 安装的符号链接,如下所示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 usingpython some_script.py
command (it depends onpy
files association). If one wants to avoid this problem it's best to create a batch fileidle.bat
with the contentpython -c "from idlelib.PyShell import main; main()"
and place it in theScripts
folder in the virtualenv. Also, like others noted idle needs bothtcl
andtk
folders to work. The simplest solution is to create symbolic links from virtualenv to the base Python installation like this我正在使用 Ubuntu 15.04 操作系统。我已经使用 virtualenv 安装了一些软件包。
因此,要运行 virtualenv 中的文件(包括这些包),我在终端中使用以下命令
(我的虚拟环境的名称是 venv):
运行 IDLE< /strong>,您可以使用 ctrl+o 键盘快捷键打开文件。
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):
After running the IDLE, you can open file using ctrl+o keyboard shortcut.
将一些答案放在一起,以下是我如何使用功能齐全的批处理文件在 Window 上执行此操作。
在 virtualenv 的 Scripts 目录中创建idle.bat。它将创建(除非它们存在)到 tcl 和 tk(撰写时为 8.5 版)的链接,并将它们放入 virtualenv 的 Lib 目录中,然后它会启动空闲状态。将此代码准确复制并粘贴到编辑器中。更改当前 virtualenv 和 Python 安装的路径名称(我的是 2.7 的标准),然后将其保存到 Scripts/idle.bat 中。
使用 Powershell(以管理员身份运行!)运行脚本以打开空闲。
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.
Run the script with Powershell (RUN AS ADMIN!) to open idle.
@biomed 我在 Windows 上,我正在尝试这个。在我的 python2.6 文件夹中,我必须将 python26/tcl/tcl8.5 和 python/tcl/tk8.5 文件夹复制到 python26/Lib 中,然后在 virtualenv 的脚本文件夹中创建上面的脚本。效果很好。
@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.
在 Windows 上:
On Windows:
对我来说,启动这样的东西就可以了(Linux 终端):(
venv 显然是你的 venv 的路径)
For me launching something like this just works (Linux terminal):
(venv is path to your venv obviously)