简体   繁体   English

将 Python 2.7 和 3.x 解释器添加到 PyCharm

[英]Adding Python 2.7 & 3.x Interpreters to PyCharm

I'm having issues adding a project interpreter to PyCharm from a new Anaconda environment.我在从新的 Anaconda 环境向 PyCharm 添加项目解释器时遇到问题。 I have Anaconda2 installed with one Python 2.7 environment (C:\Anaconda2\python.exe) that I've been using on Pycharm without issue for several months.我在一个 Python 2.7 环境 (C:\Anaconda2\python.exe) 上安装了 Anaconda2,我已经在 Pycharm 上使用了几个月没有问题。

I am attempting to add a second Python 3.6 interpreter (from C:\Anaconda2\envs\py36\python.exe) to my PyCharm.我正在尝试向我的 PyCharm 添加第二个 Python 3.6 解释器(来自 C:\Anaconda2\envs\py36\python.exe)。 After adding the Local Interpreter to Pycharm, I run into a MS Visual C++ Runtime Error R6034 "An application has made an attempt to load the C runtime library incorrectly".将本地解释器添加到 Pycharm 后,我遇到了 MS Visual C++ 运行时错误 R6034“应用程序已尝试错误地加载 C 运行时库”。

From cursory googling, it seems that there could be a runtime DLL conflict (potentially msvcr90.dll) between Python 2 & 3. All fixes I see involve editing the executable path of the application, but I don't think this is feasible for my Pycharm use case.从粗略的谷歌搜索来看,Python 2 和 3 之间似乎存在运行时 DLL 冲突(可能是 msvcr90.dll)。我看到的所有修复都涉及编辑应用程序的可执行路径,但我认为这对我来说不可行Pycharm 用例。 How do I get rid of this error, or just generally be able to use both Python 2 & 3 interpreters through my PyCharm?我如何摆脱这个错误,或者只是通常能够通过我的 PyCharm 使用 Python 2 和 3 解释器?

I think that's the problem with Anaconda and different msvc dll in the computer.我认为这是 Anaconda 和计算机中不同的 msvc dll 的问题。

You can test the conda command in the command line, to see if R6034 happens.可以在命令行中测试conda命令,看是否出现R6034。 If it happens, try the following solution:如果发生这种情况,请尝试以下解决方案:

I had a similar problem with Anaconda3 and Python27.我对 Anaconda3 和 Python27 也有类似的问题。 I solved this problem via executing the following command in cmd, outside of any conda environment:我通过在任何 conda 环境之外的 cmd 中执行以下命令解决了这个问题:

conda install msvc_runtime

After installing the packages, open a new command and test if the R6034 error still appears.安装包后,打开一个新命令并测试是否仍然出现 R6034 错误。

I had a similar issue and was able to resolve it by selecting:我有一个类似的问题,并且能够通过选择解决它:

File --> Invalid Caches / Restart...文件 --> 无效缓存/重启...

from PyCharm's main menu.从 PyCharm 的主菜单。

You may also want to double check that any Conda Environments that you have defined as Python Interpreters in PyCharm are properly configured per the docs您可能还需要仔细检查您在 PyCharm 中定义为 Python 解释器的任何 Conda 环境是否已根据文档正确配置

This issue was absolutely maddening.这个问题真是让人抓狂。 Million R6034 error windows would just keep popping up one after another if I just wanted to get help on a function.如果我只是想获得有关功能的帮助,百万个 R6034 错误窗口会一个接一个地弹出。 I researched it for months, on/off, opened tickets with JetBrains to no avail.我研究了几个月,开/关,用 JetBrains 打开票都无济于事。

If you need to have multiple versions of Anaconda, and if you have Anaconda paths in your PATH, before launching PyCharm, delete all Anaconda paths from PATH, and then start PyCharm.如果您需要多个版本的 Anaconda,并且您的 PATH 中有 Anaconda 路径,则在启动 PyCharm 之前,从 PATH 中删除所有 Anaconda 路径,然后启动 PyCharm。 You need to create a separate wrapper launcher script for PyCharm to fix PATH before PyCharm is started.在 PyCharm 启动之前,您需要为 PyCharm 创建一个单独的包装器启动器脚本来修复 PATH。 Note that alternative of starting PyCharm and then fixing interpreter and python console PATHS inside PyCharm do not really work.请注意,启动 PyCharm 然后在 PyCharm 中修复解释器和 python 控制台 PATHS 的替代方法并不真正有效。 Because PyCharm may be using a system path to access python to read documentation etc. So the only clean fix is to fix the system PATH before PyCharm starts.因为 PyCharm 可能正在使用系统路径来访问 python 以阅读文档等。所以唯一干净的修复是在 PyCharm 启动之前修复系统路径。

Once you understand what needs to be done, then you can use your own steps/tools.了解需要完成的操作后,您就可以使用自己的步骤/工具。 This worked for me:这对我有用:

  1. Create a script that modifies PATH.创建修改 PATH 的脚本。 I used Python for that, sed or any other tools are fine too.为此,我使用了 Python,sed 或任何其他工具也都可以。 The script simply examines each path element and removes it if it refers to Anaconda, and then puts it back together:该脚本简单地检查每个路径元素,如果它引用 Anaconda 则将其删除,然后将它们放回一起:

    path_cleanup.py:

     path_old = os.environ['PATH'] path_python_removed = [loc for loc in path_old.split(pathsep) if not ('python' in loc or 'Ana' in loc)] print(pathsep.join(path_python_removed))
  2. Create Powershell script to fix PATH and start PyCharm from that clean environment.创建 Powershell 脚本来修复 PATH 并从该干净的环境中启动 PyCharm。 To find PyCharm path, the simplest is to start it up the usual way, and head to Task Manager, right mouse click on pycharm64.exe process and select "open file location" to get the full path.要找到 PyCharm 路径,最简单的方法是按常规方式启动它,然后前往任务管理器,右键单击 pycharm64.exe 进程并选择“打开文件位置”以获取完整路径。 pycharm_clean.ps1

     $Env:Path=python path_cleanup.py # call the script to fix the PATH start-process $PYCHARM_PATH\pcharm64.exe -WindowStyle Hidden # enter your full path to pycharm and put it into background.
  3. You can create a shortcut to launch pycharm_clean.ps1 + you can add it to your windows start up folder to be launched upon login: %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\您可以创建一个快捷方式来启动 pycharm_clean.ps1 + 您可以将它添加到您的 Windows 启动文件夹以在登录时启动: %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\

If you use BASH inside Cygwin, then steps for path clean up require a bit more tuning, but nothing you cannot do.如果您在 Cygwin 中使用 BASH,那么路径清理步骤需要更多调整,但您无能为力。 If you need help, put a comment and I can add that script as well.如果您需要帮助,请发表评论,我也可以添加该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM