python - 使用报纸模块的 Ubuntu 脚本导入错误

标签 python ubuntu lxml python-import python-newspaper

我有一个脚本可以在本地运行,但不能在我的 Ubuntu 服务器上运行。其他脚本在两个平台上都可以正常工作,但是当我尝试在 Ubuntu 上运行这个特定的脚本时会引发导入错误。

root@ip-xxx-xx-xx-xxx:~# /usr/bin/python3.5 /opt/script_folder/main.py
Traceback (most recent call last):
  File "/opt/script_folder/main.py", line 2, in <module>
    import process
  File "/opt/script_folder/process.py", line 25, in <module>
    from tools.getImages import getImages
  File "/opt/script_folder/tools/getImages.py", line 5, in <module>
    from newspaper import Article
  File "/usr/local/lib/python3.5/site-packages/newspaper/__init__.py", line 10, in <module>
    from .api import (build, build_article, fulltext, hot, languages,
  File "/usr/local/lib/python3.5/site-packages/newspaper/api.py", line 14, in <module>
    from .article import Article
  File "/usr/local/lib/python3.5/site-packages/newspaper/article.py", line 15, in <module>
    from . import network
  File "/usr/local/lib/python3.5/site-packages/newspaper/network.py", line 14, in <module>
    from .configuration import Configuration
  File "/usr/local/lib/python3.5/site-packages/newspaper/configuration.py", line 15, in <module>
    from .parsers import Parser
  File "/usr/local/lib/python3.5/site-packages/newspaper/parsers.py", line 10, in <module>
    import lxml.etree
ImportError: No module named 'lxml.etree'

其他脚本工作正常,在我尝试使用报纸库之前我没有遇到这个问题。我尝试了以下操作:

  • pip3 安装 --upgrade newspaper3k
  • pip3 安装 --upgrade lxml
  • 添加/usr/local/lib/python3.5/site-packages到$PATH
  • 在虚拟环境中开启和关闭
  • 直接在脚本中导入 lxml.etree
  • 在 python3.5 终端上运行 - no errors thrown
  • apt-get 删除 python3.5, pip3 卸载 newspaper3k, pip3 安装 newspaper3k, apt-get安装python3.5
  • 检查站点包文件夹的权限
  • 检查了python3.5使用的默认库路径
  • 确保 hashbang #!/usr/bin/env python3.5 位于所有文件的顶部
  • sudo apt-get install python-lxml

我在这里被难住了。任何指导将不胜感激。


编辑: 我还按照此处列出的说明进行操作:https://newspaper.readthedocs.io/en/latest/ 除以下内容外,所有内容均已正确下载:

root@ip-xxx-xx-xx-xxx:~# curl https://raw.githubusercontent.com/codelucas/newspaper/master/download_corpora.py | python3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     100   657  100   657    0     0   9387      0 --:--:-- --:--:-- --:--:-- 10265
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "/usr/local/lib/python3.5/site-packages/nltk/__init__.py", line 137, in <module>
    from nltk.stem import *
  File "/usr/local/lib/python3.5/site-packages/nltk/stem/__init__.py", line 29, in <module>
    from nltk.stem.snowball import SnowballStemmer
  File "/usr/local/lib/python3.5/site-packages/nltk/stem/snowball.py", line 32, in <module>
    from nltk.corpus import stopwords
  File "/usr/local/lib/python3.5/site-packages/nltk/corpus/__init__.py", line 66, in <module>
    from nltk.corpus.reader import *
  File "/usr/local/lib/python3.5/site-packages/nltk/corpus/reader/__init__.py", line 105, in <module>
    from nltk.corpus.reader.panlex_lite import *
  File "/usr/local/lib/python3.5/site-packages/nltk/corpus/reader/panlex_lite.py", line 15, in <module>
    import sqlite3
  File "/usr/local/lib/python3.5/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.5/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: No module named '_sqlite3'

最佳答案

根据您的帖子,问题似乎出在 ubuntu 环境与您的其他(mac os?)本地环境不同。我假设您已经获得了 newspaper3k 库,可以在 Mac OS 环境中正确安装,但不能在 Ubuntu 环境中正确安装?

也许先尝试这篇文章中的指导,ImportError: No module named '_sqlite3' in python3.3

我认为核心问题是您的 ubuntu 环境缺少这些 python 库所需的一些重要的底层包。

或者也许: apt-get install sqlite3<br/> sqlite3 -version<br/> apt-get install python-pysqlite2<br/> apt-get install python-pysqlite2-dbg<br/> apt-get install libsqlite3-dev<br/> apt-get install sqlite<br/> pip install pysqlite

关于python - 使用报纸模块的 Ubuntu 脚本导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52974069/

相关文章:

python - 如何使用枚举或字符串进行过滤并获得相同的结果?

php - ubuntu 14.04 中 PHP 的 couchbase 客户端

android - 如何在linux ubuntu中提取和设置android ndk .bin文件的路径?

python - 使用 lxml HTML 解析 UTF-8/unicode 字符串

python - 使用 Pandas read_html 时遇到问题

python - 通过 gensim 将新词添加到 GoogleNews

python - jsonschema 如何将原始 json 字符串值映射到 python 对象?

python - Python while 循环中的上一个值

node.js - NODE_PATH 匹配 npm 根目录

python - 使用lxml etree在Python中将大型xml文件聚合到字典需要太长时间