小编rod*_*din的帖子

两个外部包中的python冲突

我正在编写代码来组合python rawdog RSS阅读器库和BeautifulSoup webscraping库中的函数.在我想要克服的内脏中存在冲突.

我可以用这个简化的代码复制问题:

    import sys, gzip
    def scrape(filename):
        contents = gzip.open(filename,'rb').read()
        contents = contents.decode('utf-8','replace')
        import BeautifulSoup as BS
        print 'before rawdog: ', len(BS.BeautifulSoup(contents)) # prints 4, correct answer
        from rawdoglib import rawdog as rd
        print 'after rawdog: ', len(BS.BeautifulSoup(contents)) # prints 3, incorrect answer
Run Code Online (Sandbox Code Playgroud)

无论什么顺序或我在哪里进行导入,rawdog的导入总是导致BS.BeautifulSoup()方法返回错误的响应.当我需要BeautifulSoup时,我实际上不再需要rawdog,所以我在那时尝试删除了包,但是BS仍然坏了.我尝试过的修复程序没有用到:

  • 我注意到rawdog代码自己导入了BeautifulSoup.所以我尝试import BeautifulSoup从rawdog代码中删除并重新安装rawdog
  • 在导入BeautifulSoup之前删除rawdog模块:
    • for x in filter(lambda y: y.startswith('rawdog'), sys.modules.keys()): del sys.modules[x]
  • 从rawdog导入更具体的类/方法,例如 from rawdoglib.rawdog import FeedState
  • 在导入rawdog之前和之后给问题方法一个新名称: from BeautifulSoup import BeautifulSoup as BS
  • from __future__ import absolute_import

没有运气,我总是得到len(BeautifulSoup(内容))== 3如果将rawdog导入命名空间.这两个软件包都很复杂,以至于我无法确切地知道问题重叠是什么,而且我不知道用什么工具来解决这个问题,除了搜索dir(BeautifulSoup)和dir( rawdog),我没有找到好的线索. …

python conflict packages

10
推荐指数
1
解决办法
299
查看次数

标签 统计

conflict ×1

packages ×1

python ×1