问题描述
我在Windows上工作,我发现关于conda创建的环境的导入行为有所不同,我无法理解,这里有详细信息
案例1(成功)
(基本)C:\> conda激活< env-name>
(env-name)C:\> python
>>>导入numpy
(成功)
案例2(失败)
C:\> cd<路径到conda-env>
C:路径到conda-env python
>>> import numpy
(失败)
我用 ssl遇到了这个问题
软件包,可以通过从 conda-forge
安装软件包来修复,而不是 default
,所以这似乎是软件包的问题
我麻烦的是,在我检查<$ c时,import语句似乎在conda env之外加载了某些内容$ c> sys.path 和 sys.executable
在两种情况下都是相同的
我在这里错过了什么?欢迎任何输入。
致谢
我重现了问题(与 sys.path
相同,其中排除了我最初的猜测:%PYTHONPATH%)与 Anaconda 2018.12 站在我这边。环境设置(无论是 Ancaonda , VEnv 还是任何其他此类工具)(主要)是设置一些环境变量。
在测试了更多模块(除了 numpy 和 ssl 之外)后,通过查看错误,我意识到失败的模块还有其他 .dll 自己的依赖项。考虑
重新启动控制台后,一切正常。
I am working on Windows, I find a difference in import behavior about conda created environment which I cannot understand, here the details
Case 1 (Success)
(base) C:\> conda activate <env-name>
(env-name) C:\> python
>>> import numpy
(Success)
Case 2 (Fail)
C:\> cd <path-to-conda-env>
C:\path-to-conda-env> python
>>> import numpy
(Fail)
I hit this issue with ssl
package before, and it can be fixed by install the package from conda-forge
rather then default
, so it seem to be a issue with the package
What trouble me is the import statement seem to be loading something outside my conda env as I checked sys.path
and sys.executable
is same in both case
What I have missed here? Any input are welcome.
Regards
I reproduced the problem (identical sys.path
, which excluded my initial guess: %PYTHONPATH%) on my side with Anaconda 2018.12. Environment setting (whether it's Ancaonda, VEnv or any other such tool) consists of (mainly) setting some environment variables.
After testing with some more modules (besides numpy and ssl), by looking at the errors, I realized that the modules that fail have other .dll dependencies of their own. Considering [MS.Docs]: Dynamic-Link Library Search Order, I displayed the contents of my %PATH% variable inside the Python process. On the conda enabled version, the paths below were present at the beginning:
Needless to say, that the problem disappeared after prepending those in my %PATH% before starting normal Python:
But, you should always follow the recommended way (especially when not fully aware of what's going on), and that is activating the environment, because even if this works for this scenario, it might not work for others.
@EDIT0:
As I specified in one of the comments, in order to add the environment to PyCharm, follow the steps from [SO]: How to install Python using the "embeddable zip file" (@CristiFati's answer), with some mentions:
- At step #4. make sure to select "Conda Environment" instead of "Virtualenv Environment"
Apparently, the problem persists when launching Python Console. It shouldn't be the case, seems like the environment is not set. Maybe it's because I didn't create an environment, I'm simply launching Python from the root Anaconda installation? Anyway, as a workaround (gainarie), I'm applying the same changes (setting %PATH%) for the Python Console (from "Settings -> Build, Execution, Deployment -> Console -> Python Console"), as shown in the image below:
After console restart, everythings works fine.
这篇关于康达在激活环境之外寻找图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!