推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
whx20202
V2EX  ›  Python

Python 用 pyinstalller 打包成二进制,执行时如何知道自己当前位置?

  •  
  •   whx20202 · Jul 18, 2018 · 2728 views
    This topic created in 2995 days ago, the information mentioned may be changed or developed.

    打包的时候指定了 --onefile

    sys.argv[0] 好像也不行

    目前只想到获取自己 PID,然后去系统 path 里面搜出来

    有没有更加正常一点的方法?

    Supplement 1  ·  Jul 18, 2018
    感谢各位,有时候脑子蠢了
    os.path.join(os.getcwd(), sys.argv[0])
    这么简单即可
    4 replies  •  2018-07-18 12:10:15 +08:00
    qsnow6
        1
    qsnow6  
       Jul 18, 2018   ❤️ 1
    os.getcwd()
    ysc3839
        2
    ysc3839  
       Jul 18, 2018 via Android
    Windows 下可以用 GetModuleFileName 来获取。
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197
    ysc3839
        3
    ysc3839  
       Jul 18, 2018 via Android
    ysc3839
        4
    ysc3839  
       Jul 18, 2018 via Android
    sys.argv[0] 并不可靠,因为可以传任意值进来。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1385 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 16:44 · PVG 00:44 · LAX 09:44 · JFK 12:44
    ♥ Do have faith in what you're doing.