如何从 wheel 文件导入 pyzmq?
How to import pyzmq from wheel file?
我需要从 pyzmq.whl 文件中导入 zmq,但出现了 ImportError。由于限制,我不能做 pip install
.
我已经从 pypi.org 下载了 "pyzmq-18.1.0-cp37-cp37m-manylinux1_x86_64.whl" 文件(它是 Python 3.7.4 的正确版本吗?)并在我的文件中将其重命名为 pyzmq.whl当前目录。
import sys
sys.path.append("./pyzmq.whl")
import zmq
我收到此错误消息:
File "import_zmq.py", line 3, in <module>
import zmq
File "pyzmq.whl/zmq/__init__.py", line 47, in <module>
File "pyzmq.whl/zmq/backend/__init__.py", line 40, in <module>
File "pyzmq.whl/zmq/utils/sixcerpt.py", line 34, in reraise
File "pyzmq.whl/zmq/backend/__init__.py", line 27, in <module>
File "pyzmq.whl/zmq/backend/select.py", line 28, in select_backend
File "pyzmq.whl/zmq/backend/cython/__init__.py", line 6, in <module>
ImportError: cannot import name 'constants' from 'zmq.backend.cython' (pyzmq.whl/zmq/backend/cython/__init__.py)
This question 指出这是一个文件夹结构问题,但我还没有提取 wheel 文件,所以我不确定如何解决这个错误。
编辑:没关系,可能无法将 pyzmq 作为 wheel 文件导入,因为它依赖于 CPython。参见 https://www.python.org/dev/peps/pep-0427/#is-it-possible-to-import-python-code-directly-from-a-wheel-file
...importing C extensions from a zip archive is not supported by CPython (since doing so is not supported directly by the dynamic loading machinery on any platform)
无法将 pyzmq 作为 wheel 文件导入,因为它依赖于 CPython。
Importing C extensions from a zip archive is not supported by CPython (since doing so is not supported directly by the dynamic loading machinery on any platform
https://www.python.org/dev/peps/pep-0427/#is-it-possible-to-import-python-code-directly-from-a-wheel-file
好的,这就是我现在的做法。我基本上是以编程方式使用 pip install 。此外,更改 whl 文件的名称会导致问题,因此请保留原始名称。
import subprocess, sys
try:
import zmq
except ImportError:
src_path = "path/to/folder/having/whl"
pyzmq = "pyzmq-18.1.0-cp37-cp37m-manylinux1_x86_64.whl"
target_path = "path/where/you/want/it/installed"
install_cmd = sys.executable + " -m pip install --target=" + target_path + " " + src_path + pyzmq
subprocess.call(install_cmd,shell=True)
finally:
#Sometimes "import zmq" won't work here, so do this:
import importlib
zmq = importlib.import_module("zmq")
try 块确保我们不会在 zmq 已经安装的情况下重做此安装(假设此代码重复运行)。它适用于我的特定用例,希望这对某人有所帮助。
我需要从 pyzmq.whl 文件中导入 zmq,但出现了 ImportError。由于限制,我不能做 pip install
.
我已经从 pypi.org 下载了 "pyzmq-18.1.0-cp37-cp37m-manylinux1_x86_64.whl" 文件(它是 Python 3.7.4 的正确版本吗?)并在我的文件中将其重命名为 pyzmq.whl当前目录。
import sys
sys.path.append("./pyzmq.whl")
import zmq
我收到此错误消息:
File "import_zmq.py", line 3, in <module>
import zmq
File "pyzmq.whl/zmq/__init__.py", line 47, in <module>
File "pyzmq.whl/zmq/backend/__init__.py", line 40, in <module>
File "pyzmq.whl/zmq/utils/sixcerpt.py", line 34, in reraise
File "pyzmq.whl/zmq/backend/__init__.py", line 27, in <module>
File "pyzmq.whl/zmq/backend/select.py", line 28, in select_backend
File "pyzmq.whl/zmq/backend/cython/__init__.py", line 6, in <module>
ImportError: cannot import name 'constants' from 'zmq.backend.cython' (pyzmq.whl/zmq/backend/cython/__init__.py)
This question 指出这是一个文件夹结构问题,但我还没有提取 wheel 文件,所以我不确定如何解决这个错误。
编辑:没关系,可能无法将 pyzmq 作为 wheel 文件导入,因为它依赖于 CPython。参见 https://www.python.org/dev/peps/pep-0427/#is-it-possible-to-import-python-code-directly-from-a-wheel-file
...importing C extensions from a zip archive is not supported by CPython (since doing so is not supported directly by the dynamic loading machinery on any platform)
无法将 pyzmq 作为 wheel 文件导入,因为它依赖于 CPython。
Importing C extensions from a zip archive is not supported by CPython (since doing so is not supported directly by the dynamic loading machinery on any platform https://www.python.org/dev/peps/pep-0427/#is-it-possible-to-import-python-code-directly-from-a-wheel-file
好的,这就是我现在的做法。我基本上是以编程方式使用 pip install 。此外,更改 whl 文件的名称会导致问题,因此请保留原始名称。
import subprocess, sys
try:
import zmq
except ImportError:
src_path = "path/to/folder/having/whl"
pyzmq = "pyzmq-18.1.0-cp37-cp37m-manylinux1_x86_64.whl"
target_path = "path/where/you/want/it/installed"
install_cmd = sys.executable + " -m pip install --target=" + target_path + " " + src_path + pyzmq
subprocess.call(install_cmd,shell=True)
finally:
#Sometimes "import zmq" won't work here, so do this:
import importlib
zmq = importlib.import_module("zmq")
try 块确保我们不会在 zmq 已经安装的情况下重做此安装(假设此代码重复运行)。它适用于我的特定用例,希望这对某人有所帮助。