PyCharm安装contractions、pycontractions包失败问题求助
问题描述
- 通过PyCharm终端、设置界面安装
contractions、pycontractions包均报错 - Ubuntu系统命令行可正常安装上述两个包,但PyCharm无法导入系统命令行安装的包
PyCharm安装报错日志
Collecting pycontractions Downloading pycontractions-2.0.1-py3-none-any.whl (9.6 kB) Collecting gensim>=2.0 Downloading gensim-4.1.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (24.1 MB) Collecting pyemd>=0.4.4 Downloading pyemd-0.5.1.tar.gz (91 kB) Collecting language-check>=1.0 Downloading language-check-1.1.tar.gz (33 kB) Requirement already satisfied: scipy>=0.18.1 in /home/mike/PycharmProjects/sentiment-analysis/venv/lib/python3.7/site-packages (from gensim>=2.0->pycontractions) (1.7.1) Requirement already satisfied: numpy>=1.17.0 in /home/mike/PycharmProjects/sentiment-analysis/venv/lib/python3.7/site-packages (from gensim>=2.0->pycontractions) (1.21.3) Collecting smart-open>=1.8.1 Downloading smart_open-5.2.1-py3-none-any.whl (58 kB) Building wheels for collected packages: language-check, pyemd Building wheel for language-check (setup.py): started Building wheel for language-check (setup.py): still running... Building wheel for language-check (setup.py): finished with status 'error' Running setup.py clean for language-check Building wheel for pyemd (setup.py): started Building wheel for pyemd (setup.py): finished with status 'error' Running setup.py clean for pyemd Failed to build language-check pyemd Installing collected packages: smart-open, pyemd, language-check, gensim, pycontractions Running setup.py install for pyemd: started Running setup.py install for pyemd: finished with status 'error' ERROR: Command errored out with exit status 1: command: /home/mike/PycharmProjects/sentiment-analysis/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py'"'"'; __file__='"'"'/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-8m60jjom cwd: /tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/ Complete output (35 lines): Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py", line 595, in <module> sys.exit(main()) File "/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py", line 590, in main run_setup_hooks(config) File "/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py", line 561, in run_setup_hooks language_tool_hook(config) File "/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/setup.py", line 584, in language_tool_hook download_lt() File "/tmp/pip-install-lxk5lfva/language-check_3eaf32c96573438f8c5b56e9f1c8c15c/download_lt.py", line 128, in download_lt with closing(urlopen(url)) as u: File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.7/urllib/request.py", line 563, in error result = self._call_chain(*args) File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/usr/lib/python3.7/urllib/request.py", line 755, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(*args) File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden ---------------------------------------- ERROR: Failed building wheel for language-check ERROR: Command errored out with exit status 1: command: /home/mike/PycharmProjects/sentiment-analysis/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"'; __file__='"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-za_foj8v cwd: /tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/ Complete output (20 lines): running bdist_wheel running build running build_py creating build creating build/lib.linux-x86_64-3.7 creating build/lib.linux-x86_64-3.7/pyemd copying pyemd/__about__.py -> build/lib.linux-x86_64-3.7/pyemd copying pyemd/__init__.py -> build/lib.linux-x86_64-3.7/pyemd warning: build_py: byte-compiling is disabled, skipping. running build_ext building 'pyemd.emd' extension creating build/temp.linux-x86_64-3.7 creating build/temp.linux-x86_64-3.7/pyemd x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/mike/PycharmProjects/sentiment-analysis/venv/include -I/usr/include/python3.7m -I/home/mike/PycharmProjects/sentiment-analysis/venv/lib/python3.7/site-packages/numpy/core/include -c pyemd/emd.cpp -o build/temp.linux-x86_64-3.7/pyemd/emd.o pyemd/emd.cpp:23:10: fatal error: Python.h: No such file or directory #include "Python.h" ^~~~~~~~~~ compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- ERROR: Failed building wheel for pyemd ERROR: Command errored out with exit status 1: command: /home/mike/PycharmProjects/sentiment-analysis/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"'; __file__='"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-l59ioki7/install-record.txt --single-version-externally-managed --compile --install-headers /home/mike/PycharmProjects/sentiment-analysis/venv/include/site/python3.7/pyemd cwd: /tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/ Complete output (20 lines): running install running build running build_py creating build creating build/lib.linux-x86_64-3.7 creating build/lib.linux-x86_64-3.7/pyemd copying pyemd/__about__.py -> build/lib.linux-x86_64-3.7/pyemd copying pyemd/__init__.py -> build/lib.linux-x86_64-3.7/pyemd warning: build_py: byte-compiling is disabled, skipping. running build_ext building 'pyemd.emd' extension creating build/temp.linux-x86_64-3.7 creating build/temp.linux-x86_64-3.7/pyemd x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/mike/PycharmProjects/sentiment-analysis/venv/include -I/usr/include/python3.7m -I/home/mike/PycharmProjects/sentiment-analysis/venv/lib/python3.7/site-packages/numpy/core/include -c pyemd/emd.cpp -o build/temp.linux-x86_64-3.7/pyemd/emd.o pyemd/emd.cpp:23:10: fatal error: Python.h: No such file or directory #include "Python.h" ^~~~~~~~~~ compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- ERROR: Command errored out with exit status 1: /home/mike/PycharmProjects/sentiment-analysis/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"'; __file__='"'"'/tmp/pip-install-lxk5lfva/pyemd_aa42532f0bbb45ce80073728273c4144/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-l59ioki7/install-record.txt --single-version-externally-managed --compile --install-headers /home/mike/PycharmProjects/sentiment-analysis/venv/include/site/python3.7/pyemd Check the logs for full command output. WARNING: You are using pip version 21.1.2; however, version 21.3.1 is available. You should consider upgrading via the '/home/mike/PycharmProjects/sentiment-analysis/venv/bin/python -m pip install --upgrade pip' command.
已尝试排查操作
- 执行
sudo apt-get install python3-dev,系统提示python3-dev为最新版本,可排除Python.h头文件缺失问题
解决方案
问题根因:你的PyCharm项目默认使用独立的虚拟环境(路径为/home/mike/PycharmProjects/sentiment-analysis/venv/),和系统全局Python环境相互隔离,因此全局安装的包无法被虚拟环境识别。同时安装报错分为两个独立问题:language-check包官方资源下载返回403,pyemd包本地编译时未匹配到虚拟环境对应头文件。
方案1:直接复用全局安装的包
操作步骤:
- 打开PyCharm,依次点击
File->Settings->Project: sentiment-analysis->Python Interpreter - 点击解释器右侧的齿轮图标,选择
Show All - 选中当前项目使用的虚拟环境,点击右侧的铅笔编辑图标
- 勾选
Inherit global site-packages选项,保存设置后重启PyCharm即可识别全局安装的包
方案2:直接在项目虚拟环境中安装(推荐,环境隔离性更好)
前置操作:激活虚拟环境
打开系统终端,执行以下命令激活项目对应的虚拟环境:
source /home/mike/PycharmProjects/sentiment-analysis/venv/bin/activate
修复依赖安装问题
- 先升级虚拟环境内的基础工具:
pip install --upgrade pip setuptools wheel
- 安装预编译版本的pyemd,避免本地编译报错:
pip install pyemd==0.5.1 --only-binary :all:
- 安装修复了下载403问题的language-check分支:
pip install language-check-fork
相关产品推荐
相关产品推荐

