如何从虚拟环境 (virtualenv) 启动 python Idle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4924068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:10:02  来源:igfitidea点击:

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

pythonvirtualenvpython-idle

提问by Kevin Le - Khnle

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 解释器,则可以很好地导入该包。但是,如果我启动 Idle,则无法导入该包(因为它仅在一个特定的 virtualenv 中可用而不是在全局中可用)。如何从 virtualenv 启动 Idle,以便 virtualenv 中的所有包都可用?

采纳答案by 9000

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

对于 Python 3.6+,请参阅下方 Paul Wicking 的回答

In Python prior to 3.6, IDLE is essentially

在 3.6 之前的 Python 中,IDLE 本质上是

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

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

所以你可以自己启动它,除非你在没有默认包的情况下构建了 virtualenv。

回答by Tim

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

@biomed 我在 Windows 上,我正在尝试这个。在我的 python2.6 文件夹中,我必须将 python26/tcl/tcl8.5 和 python/tcl/tk8.5 文件夹复制到 python26/Lib,然后我在我的 virtualenv 的脚本文件夹中创建了上面的脚本。工作得很好。

回答by Piotr Dobrogost

On Windows, a Python script run from command line like this some_script.pymight be run by other Python interpreter than the one used when using python some_script.pycommand (it depends on pyfiles association). If one wants to avoid this problem it's best to create a batch file idle.batwith the content python -c "from idlelib.PyShell import main; main()"and place it in the Scriptsfolder in the virtualenv. Also, like others noted idle needs both tcland tkfolders to work. The simplest solution is to create symbolic links from virtualenv to the basePython installation like this

在 Windows 上,像这样从命令行运行的 Python 脚本some_script.py可能由其他 Python 解释器运行,而不是使用python some_script.py命令时使用的解释器(这取决于py文件关联)。如果想避免这个问题,最好创建一个idle.bat包含内容的批处理文件,python -c "from idlelib.PyShell import main; main()"并将其放在Scriptsvirtualenv的文件夹中。此外,与其他人一样,空闲需要tcltk文件夹才能工作。最简单的解决方案是像这样创建从 virtualenv 到基础Python 安装的符号链接

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

回答by srock

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

将一些答案放在一起,这是我如何使用功能齐全的批处理文件在 Window 上执行此操作。

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.

在 virtualenv 的 Scripts 目录中创建 idle.bat。它将创建(除非它们存在)到 tcl 和 tk(编写时为 8.5 版)的链接,并将它们放在您的 virtualenv 的 Lib 目录中,然后它会闲置。将此代码完全复制并粘贴到编辑器中。更改当前 virtualenv 和 Python 安装的路径名(我的是 2.7 的标准),然后将其保存到 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.

使用 Powershell 运行脚本(以管理员身份运行!)以打开空闲状态。

cd c:\<path to current virtualenv>\
./Scripts/idle.bat

回答by yan

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

对我来说,启动这样的东西才有效(Linux 终端):

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

(venv is path to your venv obviously)

( venv 显然是你的 venv 的路径)

回答by Matthew Willcockson

Short answer

简答

  1. Start the virtual environment
  2. Run python -m idlelib.idle
  1. 启动虚拟环境
  2. python -m idlelib.idle

From this answer.

这个答案

Long answer

长答案

This answer assumes Python 3.

这个答案假设 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.

有几个不同的虚拟环境管理器,每个管理器在处理 python 的安装位置和运行方式方面都有略微不同的方式,如本答案中所述

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

此答案假定使用了该venv模块,并且它是按照 docs 安装的

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

注意:一些 Linux 发行版将 venv 模块打包成一个单独的包:UbuntuDebian

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

如果虚拟环境安装在my_project-venv通过python -m venv my_project-venv从文件夹内部运行调用的文件夹中my_project,则虚拟环境将位于模块创建的新文件夹中:

my_project_dir
      │
      ┝━ my_project-venv

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

在 Windows 上,使用 Python 3.7.1,文件my_project-venv夹内的文件可能如下所示:

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.bator Activate.ps1script, depending on whether cmdor PowerShell is used:

虚拟环境可以通过运行activate.batActivate.ps1脚本来启动,具体取决于是否使用cmd或 PowerShell

:: 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

注意:如果通过双击运行这些脚本,它们不会保持外壳打开。启动一个 shell,然后通过键入上述命令来运行它们,并为您的项目更改文件夹名称

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 cshor fish, activate the environment by:

然后,从任何 shellcshfish 之外,通过以下方式激活环境:

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

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

对于cshfish有对(激活虚拟环境的具体壳脚本activate.cshactivate.fish分别),他们可以像运行activate脚本。

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:

在所有操作系统上激活虚拟环境后,运行以下命令将启动 IDLE,并访问安装到虚拟环境中的软件包:

python -m idlelib.idle

回答by arsho

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

我正在使用Ubuntu 15.04操作系统。我已经使用virtualenv安装了一些软件包。

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

因此,要运行virtualenv 中的文件,包括那些包,我在终端中使用以下命令

(Name of my virtual environment is venv):

(我的虚拟环境的名称是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+okeyboard shortcut.

运行IDLE 后,您可以使用ctrl+o键盘快捷键打开文件。

回答by Paul Wicking

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

Python 3.6现代化和重构idlelib。这一变化包括对几个方法的重命名。因此,idlelib.PyShell现在必须使用idlelib.pyshell. 以下代码段基于已接受的答案,适用于任何 Python 版本:

#!/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()