简体   繁体   English

为什么我不能在 python3 中的“from scapy.all import *”之前“导入日期时间”?

[英]Why can't I “import datetime” before “from scapy.all import *” in python3?

OK, maybe i'm missing something totally obvious, but it seems that I cannot import datetime BEFORE I import "from scapy.all import *".好的,也许我遗漏了一些非常明显的东西,但似乎我无法在导入“from scapy.all import *”之前导入日期时间。 It works fine if I import datetime AFTER I import "from scapy.all import *".如果我在导入“from scapy.all import *”之后导入日期时间,它工作正常。 I don't understand this.我不明白这一点。

Example;例子; This does NOT work...这不起作用...

    #!/usr/bin/env python3
    import os
    import datetime
    from scapy.all import *
    current_time = datetime.datetime.now()
    print(current_time)

But THIS does...但这确实...

    #!/usr/bin/env python3
    import os
    from scapy.all import *
    import datetime
    current_time = datetime.datetime.now()
    print(current_time)

I'm using Arch Linux, Python3 and the latest scapy.我正在使用 Arch Linux、Python3 和最新的 scapy。

Just a guess comparing your two snippets, but I suspect you have a namespace clash (welcome to the joys of programming) and that's precisely why import * is bad practice.只是比较您的两个片段的猜测,但我怀疑您有命名空间冲突(欢迎享受编程的乐趣),这正是import *是不好的做法的原因。

scapy has a datetime method too (haven't compared the definition between the two packages), so in this context the import order does matter because the definition of datetime is overridden . scapy也有一个datetime方法(没有比较两个包之间的定义),所以在这种情况下,导入顺序确实很重要,因为datetime的定义被覆盖了。

The best would be to import just what you need eg:最好的办法是只导入你需要的东西,例如:

#!/usr/bin/env python3
import os
import datetime
from scapy.all import sr, srp
current_time = datetime.datetime.now()
print(current_time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM