【问题标题】:How to Get the Path of the executing frozen script如何获取正在执行的冻结脚本的路径
【发布时间】:2012-05-04 20:20:29
【问题描述】:

如果您从与脚本所在位置不同的目录和驱动器运行冻结的 Python 脚本(使用 py2exe 冻结),那么确定执行脚本路径的最佳方法是什么?

我尝试过的几个解决方案

inspect.getfile(inspect.currentframe())

问题:不返回完整路径。它只返回脚本名称。

os.path.abspath( __file__ )

问题:不适用于 Windows

os.path.dirname(sys.argv[0])

问题:返回空字符串。

os.path.abspath(inspect.getsourcefile(way3))

如果驱动器与密码不同,将无法工作

os.path.dirname(os.path.realpath(sys.argv[0]))

如果驱动器与密码不同,将无法工作

这是一个最小的不工作示例

D:\>path
PATH=c:\Python27\;c:\Users\abhibhat\Desktop\ToBeRemoved\spam\dist\;c:\gnuwin32\bin

D:\>cat c:\Users\abhibhat\Desktop\ToBeRemoved\spam\eggs.py
import os, inspect, sys
def way1():
    return os.path.dirname(sys.argv[0])

def way2():
    return inspect.getfile(inspect.currentframe())

def way3():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

def way4():
    try:
        return os.path.abspath( __file__ )
    except NameError:
        return "Not Found"
def way5():
    return os.path.abspath(inspect.getsourcefile(way3))

if __name__ == '__main__':
    print "Path to this script is",way1()
    print "Path to this script is",way2()
    print "Path to this script is",way3()
    print "Path to this script is",way4()
    print "Path to this script is",way5()

D:\>eggs
Path to this script is
Path to this script is eggs.py
Path to this script is D:\
Path to this script is Not Found

相关问题:

注意

如果脚本位于您正在执行的同一驱动器上,@Fenikso 的解决方案将起作用,但当它位于不同的驱动器上时,它将不起作用

【问题讨论】:

    标签: python windows path argv


    【解决方案1】:

    从另一个驱动器运行时使用 cxFreeze 的另一种方法,即使使用 PATH:

    import sys
    
    if hasattr(sys, 'frozen'):
        print(sys.executable)
    else:
        print(sys.argv[0])
    

    来自 Python:

    H:\Python\Examples\cxfreeze\pwdme.py
    

    从命令行:

    D:\>h:\Python\Examples\cxfreeze\dist\pwdme.exe
    h:\Python\Examples\cxfreeze\dist\pwdme.exe
    

    使用路径:

    D:\>pwdme.exe
    h:\Python\Examples\cxfreeze\dist\pwdme.exe
    

    【讨论】:

    • @Fenikso:这非常有效。在发布这个问题之前,我在 SO 中很少看到对同一问题的引用,但没有一个答案本身是不正确的。
    【解决方案2】:

    恕我直言,根据绝对路径执行不同操作的代码不是一个好的解决方案。 相对路径解决方案可能会更好。使用 dirname 知道相对目录和 os.sep 以实现跨平台兼容性。

    if hasattr(sys, "frozen"):
        main_dir = os.path.dirname(sys.executable)
        full_real_path = os.path.realpath(sys.executable)
    else:
        script_dir = os.path.dirname(__file__)
        main_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
        full_real_path = os.path.realpath(sys.argv[0])
    

    frozen 属性是 python 标准。

    看看 Esky : http://pypi.python.org/pypi/esky

    【讨论】:

      【解决方案3】:

      试试这个:

      WD = os.path.dirname(os.path.realpath(sys.argv[0]))
      

      这就是我使用 cx_Freeze 来获取 .exe 真正运行的目录。

      【讨论】:

      • 如果脚本存在于不同的驱动器中,这将不起作用
      • @Abhijit - 抱歉,我不明白。这是我所有冻结脚本的基本部分,它从未失败过。你能描述一个失败的例子吗?
      • 我已更新示例以包含此场景。如果驱动器不同,它只会返回您正在运行的驱动器名称。
      • @Abhijit - 嗯。实际上,这是使用 PATH 改变行为的原因。方法 2 实际上对我来说有点用 cxFreeze。
      • 您能否详细说明And the way 2 is actually kind of working for me 的含义。您是说即使驱动器不同,这也会起作用?
      猜你喜欢
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 2021-04-10
      • 2013-06-26
      • 1970-01-01
      • 2018-02-14
      • 2010-10-10
      相关资源
      最近更新 更多