【问题标题】:How to find the containing egg file from Python class?如何从 Python 类中找到包含的 egg 文件?
【发布时间】:2014-03-06 23:30:35
【问题描述】:

从 Python 2.6 开始,可以通过合并 ma​​in.py 文件直接从 python 命令行运行 egg 文件。

现在...是否可以在运行时检测当前正在执行的 python 代码是否直接从 egg 运行并获取它的路径?

我正在尝试镜像类似的 Java 功能,可以通过 Java 的 ClassLoader 从类中获取包含 jar。

【问题讨论】:

    标签: java python module egg


    【解决方案1】:

    我没有找到更好的 Pythonic 方式来手动执行此操作:

    puria@spaghetti:~$ cat __main__.py 
     import zipfile, os
     module_path = os.path.dirname(__file__)
     print "Is this an egg? ", zipfile.is_zipfile(module_path)
    
    puria@spaghetti:~$ python __main__.py 
     Is this an egg?  False
    
    puria@spaghetti:~$ zip example.egg __main__.py 
     adding: __main__.py (deflated 15%)
    
    puria@spaghetti:~$ python example.egg 
     Is this an egg?  True
    

    希望这很有用,我实际上理解了你的问题。

    【讨论】:

    • 是的,这是完美的!抱歉,现在没有信用可以投票回答了:-(
    • @user2123288 谢谢。你可以接受我的回答 plz ;)
    猜你喜欢
    • 2015-07-21
    • 2021-01-15
    • 2017-09-26
    • 2012-09-25
    • 2011-05-01
    • 2015-08-27
    • 2010-10-25
    • 2011-04-08
    • 2011-01-18
    相关资源
    最近更新 更多