anaconda创建虚拟机环境后,使用pip命令安装依赖报Error: DLL load failed while importing _ctypes: 找不到指定的模块。
问题描述:
在使用pip安装pytorch遇到下面的错误:
from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: 找不到指定的模块。
(pt31011) PS C:\Users\MECHREVO\Downloads\pip-23.1.2> pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
Traceback (most recent call last):
File "F:\Anaconda3\envs\pt31011\Scripts\pip-script.py", line 33, in <module>
sys.exit(load_entry_point('pip==23.1.2', 'console_scripts', 'pip')())
File "F:\Anaconda3\envs\pt31011\Scripts\pip-script.py", line 25, in importlib_load_entry_point
return next(matches).load()
File "F:\Anaconda3\envs\pt31011\lib\importlib\metadata\__init__.py", line 171, in load
module = import_module(match.group('module'))
File "F:\Anaconda3\envs\pt31011\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\cli\main.py", line 9, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\cli\autocompletion.py", line 10, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\cli\main_parser.py", line 9, in <module>
from pip._internal.build_env import get_runnable_pip
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\build_env.py", line 20, in <module>
from pip._internal.cli.spinners import open_spinner
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\cli\spinners.py", line 9, in <module>
from pip._internal.utils.logging import get_indentation
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\utils\logging.py", line 29, in <module>
from pip._internal.utils.misc import ensure_dir
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\utils\misc.py", line 42, in <module>
from pip._internal.locations import get_major_minor_version
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\locations\__init__.py", line 14, in <module>
from . import _sysconfig
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\locations\_sysconfig.py", line 11, in <module>
from .base import change_root, get_major_minor_version, is_osx_framework
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\locations\base.py", line 9, in <module>
from pip._internal.utils import appdirs
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_internal\utils\appdirs.py", line 13, in <module>
from pip._vendor import platformdirs as _appdirs
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_vendor\platformdirs\__init__.py", line 42, in <module>
PlatformDirs = _set_platform_dir_class() #: Currently active platform
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_vendor\platformdirs\__init__.py", line 21, in _set_platform_dir_class
from pip._vendor.platformdirs.windows import Windows as Result
File "F:\Anaconda3\envs\pt31011\lib\site-packages\pip\_vendor\platformdirs\windows.py", line 3, in <module>
import ctypes
File "F:\Anaconda3\envs\pt31011\lib\ctypes\__init__.py", line 8, in <module>
from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: 找不到指定的模块。
解决方案:
网上解决方案参考链接:(没有解决问题)
from _ctypes import Union, Structure, ArrayImportError: DLL load failed while importing _ctypes
解决“ImportError: DLL load failed while importing _ctypes”的错误
Anaconda创建python3.10环境 ,pip报错:ImportError: DLL load failed while importing _ctypes: 找不到指定的模块。
以上链接中的解决方法博主均试验了,都没有解决问题。
最终方案:(已解决)
参考链接:
Anaconda大坑 | Python版本显示不正确 | 虚拟环境没有bin文件夹
上述链接虽然跟本问题不相关,但是对于本问题的解决起到了关键作用。
1、删除已经创建的虚拟环境
conda remove -n your_env_name --all
2、恢复anaconda默认源
conda config --remove-key channels
注意:
如果想切换回国内环境,可以参考下面的链接:
Anaconda换源和恢复默认源
3、重新创建虚拟环境
conda create -n your_env_name python=3.10.13
4、测试
能出现下图所示样式就证明成功了。
(fedl_env) C:\Windows\System32>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.
General Options:
-h, --help Show help.
--debug Let unhandled exceptions propagate outside the main subroutine, instead of logging them
to stderr.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
--require-virtualenv Allow pip to only run in a virtual environment; exit with an error otherwise.
--python <python> Run pip with the specified Python interpreter.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to
WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--no-input Disable prompting for input.
--keyring-provider <keyring_provider>
Enable the credential lookup via the keyring library if user input is allowed. Specify
which mechanism to use [disabled, import, subprocess]. (default: disabled)
--proxy <proxy> Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
(a)bort.
--trusted-host <hostname> Mark this host or host:port pair as trusted, even though it does not have valid or any
HTTPS.
--cert <path> Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL
Certificate Verification' in pip documentation for more information.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the
certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output.
--no-python-version-warning
Silence deprecation warnings for upcoming unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be removed in the future.
总结
在遇到该问题时上网查了很多解决方案,但是都没有用,搞了一下午😂,不过好在最终得到了完美解决。
我在猜想可能是因为国内源的更新速度和官网不匹配,所以在创建环境的时候会缺少关键文件的安装。python3.9以下使用国内源创建虚拟环境估计没有问题,还没有进行测试,因为在跑代码时间上不充裕。