简体   繁体   English

跨平台的方法来获取Python控制台脚本的默认目录?

[英]Cross-platform way to get default directory for Python console scripts?

Is there cross-platform way to get default directory for console scripts the same way one can use sys.executable to get path to the Python's interpreter in cross-platform way? 是否有跨平台的方法来获取控制台脚本的默认目录,就像可以使用sys.executable以跨平台的方式获取通往Python解释器的路径一样?

Context: 内容:
There's Python script which runs various tools like pip using subprocess module and it would be good to make sure to run tools that accompany Python's interpreter used to run this script. 有Python脚本可以使用子进程模块运行各种工具,例如pip,并且最好运行与Python解释器一起运行的工具,以运行该脚本。 Please note that the script may be run from within virtualenv which had not been activated – ./venv/bin/python script.py 请注意,该脚本可能是在尚未激活的virtualenv中运行的./venv/bin/python script.py

Yes, sysconfig module provides such API through get_path() function: 是的, sysconfig模块通过get_path()函数提供了此类API:

Linux: Linux:

pdobrogost@host:~/tmp$ venv/bin/python -c "import sysconfig; print(sysconfig.get_path('scripts'))"
/home/users/pdobrogost/tmp/venv/bin

Windows: 视窗:

X:\>venv\Scripts\python.exe -c "import sysconfig; print(sysconfig.get_path('scripts'))"
X:\venv\Scripts

References: 参考文献:
https://groups.google.com/forum/#!topic/comp.lang.python/s3vLkVy2xJk https://groups.google.com/forum/#!topic/comp.lang.python/s3vLkVy2xJk
https://mail.python.org/pipermail/distutils-sig/2014-February/023867.html https://mail.python.org/pipermail/distutils-sig/2014-February/023867.html

Thank you Ned Batchelder, Oscar Benjamin, Paul Moore and Vinay Sajip for help. 谢谢Ned Batchelder,Oscar Benjamin,Paul Moore和Vinay Sajip的帮助。

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

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