简体   繁体   English

python 脚本的不同 shebang 行取决于系统

[英]Different shebang line for a python script depending on the system

I've a python script using the Django framework running from a virtualenv, and I'd like to use the same script on a different machine where the virtualenv is located in a different place.我有一个 python 脚本,它使用从 virtualenv 运行的 Django 框架,我想在 virtualenv 位于不同位置的不同机器上使用相同的脚本。

The script is like this:脚本是这样的:

#!/home/lenovo/.virtualenvs/gjt/bin/python
sys.path.append('/home/lenovo/prj/gjt/gjt')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gjt.settings')
import django
django.setup()

To use the same script on a different machine, I'd need to change the shebangline to something else.要在不同的机器上使用相同的脚本,我需要将 shebangline 更改为其他内容。

  1. Either can I define the shebang line based on the /etc/hostname file我可以根据 /etc/hostname 文件定义 shebang 行吗
  2. or somehow use #,/usr/bin/python as the shebang?或者以某种方式使用#,/usr/bin/python 作为shebang? and still be able to use the virtualenv from the mentioned location.并且仍然可以从提到的位置使用 virtualenv。 Of course i need to change the sys.path,append accordingly.当然我需要相应地更改 sys.path,append。 but that's not an issue.但这不是问题。

Thanks.谢谢。

This shebang should be suitable for different vitual environments:这个shebang应该适合不同的虚拟环境:

#!/usr/bin/env python

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

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