同时更新多个项目中的 Python 解释器 [英] Updating Python interpreter in multiple projects at the same time

查看:62
本文介绍了同时更新多个项目中的 Python 解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PyCharm,可以在同一个窗口/环境中拥有多个项目.每个项目在 Project > 中都有自己的解释器配置.Python 解释器部分.

有没有一种简单的方法可以一次将所有项目切换到同一个解释器?快速切换器"在状态栏中只更新任何被认为是当前项目"的内容.(包含当前或上次打开文件的项目).切换到单个项目和多个内容根目录不是一种选择,因为它会导致多个项目解决的各种其他问题.

解决方案

问题中描述的案例是在同一窗口中打开多个项目.如

最后,为了在 IDE 中为在项目视图中打开的所有项目同时更改解释器,您可以定义一个 自定义范围 并在 Edit > Find > 中使用它在文件中替换(或Ctrl + Shift + R)限制对您打开的项目的.idea\your_project_name.iml 文件的更改.

如果您已经为所有项目设置了相同的解释器,那么进行更改会变得特别容易.在上面的例子中,替换行 到定义的自定义范围内的新解释器的代码将一键生效.

Using PyCharm, it's possible to have multiple projects in the same window / environment. Each project has its own interpreter configuration in the Project > Python Interpreter section.

Is there an easy way to switch all projects to the same interpreter at once? The "quick switcher" in the status bar only updates whatever is considered the "current project" (the project containing the current or last open files). Switching to a single project and multiple content roots is not an option as it leads to various other problems which multiple projects solved.

解决方案

The case described in the question is of having several projects opened in the same window. As described in Managing multiple projects - PyCharm documentation.

The UI currently does not offer the functionality to simultaneously change the interpreter for several projects opened in the same project window. So the only option left (besides picking the interpreter individually for each project in the Settings > Project Interpreter UI dialogue) would be to edit the IDE project configuration files.

The interpreter for each individual project is hard coded in each project's .idea folder inside the .iml file, for example (some irrelevant lines truncated for legibility):

project_folder\.idea\your_project_name.iml

<module type="PYTHON_MODULE" version="4">
  <component name="NewModuleRootManager">
    <orderEntry type="jdk" jdkName="Python 3.8 (venv38)" jdkType="Python SDK" />
  </component>
</module>

Notice in the element <orderEntry> the attributes type="jdk", " jdkName="Python 3.8 (venv38)", etc, define the interpreter that project is set to use. By changing this line in the individual .iml files you are setting that project's interpreter.

The list of Python Interpreters in Settings > Project Interpreter, if you press the cog and Show All..., is populated (in Windows) from the file C:\Users\your_user\AppData\Roaming\JetBrains\PyCharm2020.version\options\jdk.table.xml there you'll find the XML elements corresponding to each interpreter you've added in the past, for example (some irrelevant lines truncated for legibility):

<application>
  <component name="ProjectJdkTable">
    <jdk version="2">
      <name value="Python 3.8 (venv38)" />
      <type value="Python SDK" />
      <version value="Python 3.8.0" />
      <homePath value="C:\path_to_venv38\Scripts\python.exe" />
    </jdk>
</component>
</application>

Finally, in order to change the interpreter simultaneously for all projects opened in a Project View from within the IDE, you could define a custom scope and use it in Edit > Find > Replace in Files ( or Ctrl + Shift + R) restricting the change to the .idea\your_project_name.iml files of your opened projects.

Making the change becomes especially easy if you already set the same interpreter for all projects once. In the above example, replacing the line <orderEntry type="jdk" jdkName="Python 3.8 (venv38)" jdkType="Python SDK" /> to that of the new interpreter within the defined custom scope would effect the change in one click.

这篇关于同时更新多个项目中的 Python 解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆