Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

pycharm - Updating Python interpreter in multiple projects at the same time

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.ideayour_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:Usersyour_userAppDataRoamingJetBrainsPyCharm2020.versionoptionsjdk.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_venv38Scriptspython.exe" />
    </jdk>
</component>
</application>

image of Python interpreters in Settings

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...