Python 3.x 尝试从Python3全局类获取文件名或目录,然后使用bash脚本执行 我有一个globals.py文件,我想存储整个系统的相关信息,如目录位置、python3服务/脚本的路径等。 我使用bashshell脚本在启动时启动这些服务,但是我需要从全局类枚举中检索服务位置。
我不断收到shell错误: python3:无法打开文件'path/to/service.py':[Errno 2]没有这样的文件或目录 我已经尝试了上述所有形式的服务路径。检索方法为:Python 3.x 尝试从Python3全局类获取文件名或目录,然后使用bash脚本执行 我有一个globals.py文件,我想存储整个系统的相关信息,如目录位置、python3服务/脚本的路径等。 我使用bashshell脚本在启动时启动这些服务,但是我需要从全局类枚举中检索服务位置。,python-3.x,linux,bash,Python 3.x,Linux,Bash,我不断收到shell错误: python3:无法打开文件'path/to/service.py':[Errno 2]没有这样的文件或目录 我已经尝试了上述所有形式的服务路径。检索方法为: **launch.sh** function get_service() { python3 -c "import common.globals as gbl; print(gbl.SERVICE_MAP['$1'])" } 然后打电话: **launch.sh** servic
**launch.sh**
function get_service() {
python3 -c "import common.globals as gbl; print(gbl.SERVICE_MAP['$1'])"
}
然后打电话:
**launch.sh**
service=$(get_service "test") # get service "test"
echo $service #Output: is what it should be... "path/to/service.py"
# but when I now do:
python3 $service
#or
python3 "$service"
Output is always:
python3: can't open file '/path/to/service.py': [Errno 2] No such file or directory
我做错了什么?我没有在globals.py中使用完整路径,而是使用相对路径:
SERVICE_MAP = {
'test': '/rel/path/to/service.py',
}
在我的launch.sh脚本中,我预先添加了$HOME:
我最喜欢这个,因为$HOME用户可能有一天会改变。。。回答了我自己的问题,谢谢看 ~/path/to/service.py!=路径/to/service.py!=/路径/to/service.py。。。这是哪一个?我个人认为test3格式的成功概率最高。。。
SERVICE_MAP = {
'test': '/rel/path/to/service.py',
}
function get_service() {
python3 -c "import common.globals as gbl; print(gbl.SERVICE_MAP['$1'])"
}
python3 "$HOME$(get_service "test")"