使用报纸模块在Ubuntu脚本中导入错误

时间:2018-10-24 16:38:16

标签: 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安装-升级报纸3k
  • pip3 install --upgrade lxml
  • /usr/local/lib/python3.5/site-packages 添加到$ PATH
  • 在虚拟环境中运行和运行
  • 直接在脚本中导入lxml.etree
  • 在python3.5终端上运行- no errors thrown
  • apt-get删除python3.5, pip3卸载报纸3k, pip3安装报纸3k, 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'

1 个答案:

答案 0 :(得分:0)

根据您的帖子,看来问题出在ubuntu环境可能不同于您的其他(mac os?)本地环境。我认为您已经获得了报纸3k lib可以在Mac OS环境中正确安装,但不能在Ubuntu上安装

也许首先尝试这篇帖子中的指南,ImportError: No module named '_sqlite3' in python3.3

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

或者: apt-get install sqlite3
sqlite3 -version
apt-get install python-pysqlite2
apt-get install python-pysqlite2-dbg
apt-get install libsqlite3-dev
apt-get install sqlite
pip install pysqlite