简体   繁体   中英

conda python can't find library from command line, ImportError: DLL load failed: The specified module could not be found

my python code works in spyder, but errors when run in cmd:

C:\Users\XYZ\AppData\Local\Continuum\anaconda3>python.exe "C:\\Users\\XYZ\\project_1\\Model\\PPP_2_main.py"

Traceback (most recent call last):
  File "C:\\Users\\XYZ\\project_1\\Model\\PPP_2_main.py", line 8,
 in <module>
    import numpy as np
  File "C:\Users\XYZ\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\XYZ\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.

I read this and it says python can't locate the module, and we need to add sys path. But isn't the error msg mentioning the correct path to the numpy library?

Confirm the path to library is correct (in sypder):

import numpy 
numpy.__file__
Out[5]: 'C:\\Users\\XYZ\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'

Check conda env:

(base) C:\Users\XYZ>conda env list
# conda environments:
#
base                  *  C:\Users\XYZ\AppData\Local\Continuum\anaconda3

The path of the base env is where i was when running the code in cmd


I read this and tried to activate conda, and it didn't work. In conda cmd:

(base) C:\Users\zhayuji>conda activate base

(base) C:\Users\zhayuji\AppData\Local\Continuum\anaconda3>conda activate base

(base) C:\Users\zhayuji\AppData\Local\Continuum\anaconda3>conda list
# packages in environment at C:\Users\zhayuji\AppData\Local\Continuum\anaconda3:

#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0
_py-xgboost-mutex         2.0                       cpu_0
alabaster                 0.7.12                   py37_0
anaconda-client           1.7.2                    py37_0
anaconda-navigator        1.9.7                    py37_0    anaconda
....
numpy                     1.16.2           py37h19fb1c0_0
numpy-base                1.16.2           py37hc3f5095_0
numpydoc                  0.9.1                      py_0

After this i reopen cmd and ran python code, and it's the same err as at the beginning of the post.

I just checked with a colleague who had essentially the same problem, I think the mentioned link is correct.

Step 1 Edit Environment Variables - you can use this for your Account without requiring Admin Access (at least in Windows 10) 在此处输入图片说明

Then you add these paths, I am rather confident that the library/bin one is the most relevant here as this is the problem. Your python executed from the windows command line does not know where to find the additional libraries. 在此处输入图片说明

You might need to open a new command line interface - then it should work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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