简体   繁体   English

Jython,如何获取脚本所在的jar的基本路径?

[英]Jython, how to get the base path of the jar a script is in?

I've packaged some python scripts in a jar using the method described here: Distributing my Python scripts as JAR files with Jython? 我已经使用此处描述的方法将一些python脚本打包到了jar中: 使用Jython将我的Python脚本作为JAR文件分发吗?

This worked fine, but I'm having trouble getting to the base path of the jar my script is in. At the moment, the workaround I've found was to cd to the directory the jar is in and retrieve the path with os.getcwd(). 这个工作正常,但是我无法进入脚本所在的jar的基本路径。目前,我发现的解决方法是将cd cd到jar所在的目录并使用os检索路径。 GETCWD()。

Using os.path.realpath(__file__) gives me /path/to/jar/myapplication.jar/Lib/module/file.py. 使用os.path.realpath(__file__)给我/path/to/jar/myapplication.jar/Lib/module/file.py。

Is there a variable like __file__ that would give me /path/to/jar/myapplication.jar instead ? 是否有一个类似__file__的变量代替了/path/to/jar/myapplication.jar

The reason is I have some configuration file externalized that I would like to retrieve based on a relative path to my application. 原因是我有一些要外部化的配置文件,我想根据我的应用程序的相对路径来检索该配置文件。

似乎sys.exec_prefix是我要寻找的。

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

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