Python DNS模块导入错误

时间:2014-02-08 03:57:14

标签: python python-2.7 module resolver

我一直在使用python dns模块。我试图在新的Linux安装上使用它,但模块没有加载。 我试图清理和安装,但安装似乎没有工作。


    $ python --version
    Python 2.7.3
    $ sudo pip install dnspython
    Downloading/unpacking dnspython
      Downloading dnspython-1.11.1.zip (220Kb): 220Kb downloaded
      Running setup.py egg_info for package dnspython

    Installing collected packages: dnspython
      Running setup.py install for dnspython

    Successfully installed dnspython
    Cleaning up...
    $ python
    Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dns
    Traceback (most recent call last):
      File "", line 1, in 
    ImportError: No module named dns

更新了python版本和pip版本命令的输出


    $ which python
    /usr/bin/python
    $ python --version
    Python 2.7.3
    $ pip --version
    pip 1.0 from /usr/lib/python2.7/dist-packages (python 2.7)

非常感谢你的帮助。

注意: - 我在新机器上安装了防火墙。我不确定它是否会影响导入。但我试过禁用它,但它似乎无法正常工作。

15 个答案:

答案 0 :(得分:25)

我遇到了与dnspython相同的问题。

我的解决方案是从官方GitHub项目构建源代码。

所以我的步骤是:

git clone https://github.com/rthalley/dnspython
cd dnspython/
python setup.py install

完成此操作后,我可以导入dns模块。

编辑

似乎pip安装对此模块不起作用。按照描述从源代码安装。

答案 1 :(得分:5)

我通过卸载然后使用PIP重新安装dnspython模块解决了这个问题。

$ pip uninstall dnspython

在pycache中的长文件列表之后,键入y以继续卸载。完成后输入:

$ pip install dnspython

然后我运行了我的脚本,错误得到了解决。

答案 2 :(得分:3)

我使用pip install dnspython在我的Ubuntu框中安装了 dnspython 1.11.1 。我能够毫无问题地导入dns模块

我在基于Ubuntu的服务器上使用Python 2.7.4。

答案 3 :(得分:3)

您也可以使用以下命令安装pip包:

pip install git+https://github.com/rthalley/dnspython

答案 4 :(得分:2)

关于Debian 7 Wheezy,我必须这样做:

pip install --upgrade dnspython

即使安装了python-dns软件包。

答案 5 :(得分:1)

你正在使用的pip版本很可能没有安装到你正在使用的python版本。我有一个盒子就是这种情况......

尝试:

which python

python --version

pip -V

如果它看起来像pip与你的python不匹配,那么你可能有类似我在我的盒子上找到的python和pip的多个版本......

[root@sdpipeline student]# locate bin/pip

/home/student/class/bin/pip

/home/student/class/bin/pip-2.7

/usr/bin/pip

/usr/bin/pip-python

只要我使用/ home / student / class / bin / pip(在该框中匹配我的python版本的2.7),那么我的导入工作正常。

您也可以尝试从源代码安装pip:http://www.pip-installer.org/en/latest/installing.html

可能有更好的方法来做到这一点,我仍然在学习我的方式,但这就是我解决它的方式 - 希望它有所帮助!

答案 6 :(得分:1)

我遇到了同样的问题,并按如下所述解决了这个问题: 由于您已经成功下载并安装了dnspython,所以

  1. 进入文件夹dnspython
  2. 您将找到dns目录,现在将其复制
  3. 然后将其粘贴到site-packages目录内

仅此而已。现在你的问题就解决了

如果未安装dnspython,则可以这样安装:

  1. 转到您的python安装文件夹site-packages目录
  2. 在此处打开cmd并输入命令: import pickle # coding conventions: pseudo-constants should be ALL_UPPER NPCS_PICKLE_FILE = 'NPCatt.pk' NPC_COUNT = 200 # coding convention: class names should be CamelCase class Npc: def __init__(self, name="", occupation="", weakness="", need="", desire="", enemy="", rumor="", secret="", passion="", redeeming_quality="",damning_quality="", happy="", occ_desire="", occ_complication="", pc_opinion="", accomplishment="", magical_gear="", political_influence="", resource="", intel="", research=""): # Attributes self.name = name self.occupation = occupation self.weakness = weakness self.need = need self.desire = desire self.enemy = enemy self.rumor = rumor self.secret = secret self.passion = passion self.redeeming_quality = redeeming_quality self.damning_quality = damning_quality self.happy = happy self.occ_desire = occ_desire self.occ_complication = occ_complication self.pc_opinion = pc_opinion self.accomplishment = accomplishment self.magical_gear = magical_gear self.political_influence = political_influence self.resource = resource self.intel = intel self.research = research def update(self, **values): for key, val in values.items(): # make sure we're only accepted known attributes if not hasattr(self, key): raise AttributeError("Npc object has no attribute '{}'".format(key)) setattr(self, key, val) def __str__(self): # in Python, string concatenation is better done by # building a list and joining it afterward npc_output = ["####NPC SUMMARY####"] for att, val in self.__dict__.items(): if val: npc_output.append(f"{att} = {val}") npc_output.append("") # so we have a last newline return "\n".join(npc_output) class InvalidDataFile(ValueError): pass def load_npcs_from_file(): with open(NPCS_PICKLE_FILE, 'rb') as fi: try: npcs = pickle.load(fi) except EOFError as e: raise InvalidDataFile( "looks like {} is empy - expected a dict, got {}".format(NPCS_PICKLE_FILE, e) ) # make sure we don't have incorrect data from # the previous version where what was pickled was a list if not isinstance(npcs, dict): raise InvalidDataFile( "looks like {} is obsolete or corrupted - expected a dict, got {}".format(NPCS_PICKLE_FILE, ncps) ) # make sure we ALWAYS have `NPC_COUNT` npcs whatever # (ie: in case the pickle didn't have as many entries as expected) missing = NPC_COUNT - len(npcs) if missing > 0: for id in range(NPC_COUNT): ncps.setdefault(id, None) return npcs def init_npcs(): return {id: None for id in range(NPC_COUNT)} def load_npcs(): try: return load_npcs_from_file() except (FileNotFoundError, InvalidDataFile) as e: # so you know what's happening... print("got {} when trying to load npcs from file - creating a new dataset".format(e)) return init_npcs() def save_npcs(npcs): with open(NPCS_PICKLE_FILE, 'wb') as fi: # dump your data into the file pickle.dump(npcs, fi) def get_npc_values(): # factor out common stuff # XXX you definitly want to validate user inputs values = {} values["name"] = input("Enter name of NPC: ") values["occupation"] = input("Enter NPC occupation: ") values["weakness"] = input("Enter Weakness: ") values["need"] = input("Enter Need: ") values["desire"] = input("Enter Desire: ") values["enemy"] = input("Enter Enemy: ") values["rumor"] = input("Enter Rumor: ") values["secret"] = input("Enter Secret: ") values["passion"] = input("Enter Passion: ") values["redeeming_quality"] = input("Enter Redeeming Quality: ") values["damning_quality"] = input("Enter Damning Quality: ") values["happy"] = input("Enter, is this NPC happy?: ") values["occ_desire"] = input("Enter an Occupational Desire: ") values["occ_complication"] = input("Enter an Occupational Complication: ") values["pc_opinion"] = input("Enter this NPC's disposition toward the PCs: ") values["accomplishment"] = input("Enter an Accomplishment: ") values["magical_gear"] = input("Enter Magical Gear: ") values["political_influence"] = input("Enter Political Influence: ") values["resource"] = input("Enter Resource Level: ") values["intel"] = input("Enter Intel Tier: ") values["research"] = input("Enter Research: ") return values def update_npc(npc): new_values = get_npc_values() npc.update(**new_values) def create_npc(): values = get_npc_values() return Npc(**values) def get_npc_id(): #select an NPC to modify / create npc_id = None while npc_id is None: try: npc_id = int(input(f"Enter the id number of the NPC you wish to modify: ")) except ValueError as ve: print("You must provide a numerical id") if npc_id < 0 or npc_id >= NPC_COUNT: npc_id = None print(f"you must provide a value between 0 and {NPC_COUNT}") return npc_id def edit_npc(npcs): npc_id = get_npc_id() # this should be safe now... theoretically at least npc = npcs[npc_id] if npc is None: ok = input("This NPC doesn't exist yet, do you want to create it (y/n): ") if ok.strip().lower() == 'y': npcs[npc_id] = create_npc() # let the caller know something was changed return True else: ok = input("This NPC already exists, do you want to continue and change them? (y/n): ") if ok.strip().lower() == "y": update_npc(npc) # let the caller know something was changed return True # let the caller know nothing was changed return False def show_npcs(npcs): for id, npc in npcs.items(): print("{} : {}".format(id, npc)) print("") # add a blank line def get_next_action(): while True: print("what do you want to do ?") print("S - show existing npcs") print("E - edit a npc") print("Q - quit") action = input(">> ").strip().upper() if action not in "SEQ": print("sorry, I don't undertand '{}'".format(action)) return action def main(): npcs = load_npcs() while True: nb_valids = len([_ for _ in npcs.values() if _]) print("we have {} valid npcs".format(nb_valids)) action = get_next_action() if action == "Q": break elif action == "E": if edit_npc(npcs): print("saving data") save_npcs(npcs) elif action == "S": show_npcs(npcs) if __name__ == "__main__": main()

现在,dnspython将成功安装。

答案 7 :(得分:0)

Symantec End Point Protection(SEP)可以生成此问题。 我怀疑大多数EPP产品可能会影响您的脚本运行。

如果禁用SEP,脚本将立即运行。

因此,您可能需要更新SEP策略,以阻止python脚本访问内容。

答案 8 :(得分:0)

我从github源安装了DNSpython 2.0.0,但是运行“ pip list”显示了dnspython 1.2.0的旧版本。

只有在我运行“ pip卸载dnspython”后,它才删除旧版本,仅保留2.0.0,然后“导入dns”运行顺利。

答案 9 :(得分:0)

一个可能的原因是您的脚本 shebang 错误(因此它没有使用virtualenv中的python)。我只是做了这个更改,就可以了:

-#!/bin/python
+#!/usr/bin/env python

或者忽略shebang,然后在venv中使用python运行脚本:

$ python your_script.py

答案 10 :(得分:0)

使用“ import dns.resolver”时出现错误。我尝试了dnspython,py3dns,但它们失败了。 DNS将无法安装。经过多次尝试后,我安装了pubdns模块,它解决了我的问题。

答案 11 :(得分:0)

就我而言,我已经在名为“ dns.py”的文件中编写了代码,这与程序包有冲突,我必须重命名脚本文件名。

答案 12 :(得分:0)

确定解决此问题的方法首先使用pip install dnspython通过cmd为python安装dns
(如果您使用conda,请首先键入activate,然后以base(以cmd为单位),然后键入上面的代码) 它将安装在anaconda网站包中,从cmd复制该网站包文件夹的位置,然后将其打开。现在,复制所有dns文件夹并将其粘贴到python网站包文件夹中。它会解决它。

实际上,我们的代码无法在蟒蛇\站点软件包bcz中找到指定的软件包,而在anaconda \ site软件包中。因此,您必须复制它(不剪切)。

答案 13 :(得分:0)

我在 mac 上导入时遇到了类似的问题。我安装了 python 3.7.3 以下步骤帮助我解决了这个问题:

  1. pip3 卸载 dnspython
  2. sudo -H pip3 安装 dnspython

导入 dns

导入 dns.resolver

答案 14 :(得分:0)

如果您没有(或不想)安装 pip,还有另一种方法。您可以通过使用本机操作系统包管理器安装包来解决此问题。

例如对于基于 Debian 的系统,这将是命令:

apt install python3-dnspython