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

发布于 2024-10-16 06:31:13 字数 158 浏览 11 评论 0原文

我有一个从虚拟环境安装的软件包。如果我只是启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

书间行客 2024-10-23 06:31:13

简短回答

  1. 启动虚拟环境
  2. 运行python -midlelib.idle

这个答案

长答案

这个答案假设Python 3。

有几种不同的虚拟环境管理器,每种管理器处理Python安装位置和运行方式的方式略有不同,如本答案所述

此答案假设 venv 模块,并且已安装 遵循文档

注意:某些 Linux 发行版将 venv 模块打包到单独的包中: UbuntuDebian

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

my_project_dir
      │
      ┝━ my_project-venv

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

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

可以通过运行 activate.batActivate.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:

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

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

在大多数其他操作系统上,虚拟环境文件夹将如下所示:

my_project-venv
      │
      ┝━ bin
      │     ┝━ ...
      │     ┝━ activate
      │     ┝━ activate.csh
      │     ┝━ activate.fish
      │     ┕━ ...
      │
      ┝━ include
      ┝━ lib
      ┝━ lib64
      ┕━ pyvenv.cfg

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

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

对于 csh< /code> 和 fish 有用于激活虚拟环境的特定于 shell 的脚本(分别为 activate.cshactivate.fish),它们可以像 activate 脚本一样运行。

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

python -m idlelib.idle

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
左耳近心 2024-10-23 06:31:13

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

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

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

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

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.

予囚 2024-10-23 06:31:13

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()

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()
鹊巢 2024-10-23 06:31:13

在 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 安装的符号链接,如下所示

(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

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
风柔一江水 2024-10-23 06:31:13

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

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

(我的虚拟环境的名称是 venv):

#Activate the virtualenv venv
source venv/bin/activate

#To Run IDLE in virtualenv venv
python -m idlelib

运行 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):

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

萌面超妹 2024-10-23 06:31:13

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

在 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()"

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

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

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
三生路 2024-10-23 06:31:13

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

几味少女 2024-10-23 06:31:13

在 Windows 上:

C:\foo\bar\{venv_name}\activate
python -m idlelib

On Windows:

C:\foo\bar\{venv_name}\activate
python -m idlelib
单身情人 2024-10-23 06:31:13

对我来说,启动这样的东西就可以了(Linux 终端):(

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

venv 显然是你的 venv 的路径)

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

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

(venv is path to your venv obviously)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文