How to use path of current conda environment's python as shebang for a script?

半世苍凉 提交于 2020-01-23 09:51:06

问题


Let's say you have 2 conda environments: py3_env and py3_clone_env

If you have a script.py with the following structure:

#![shebang]
import sys
def main():
    print("hello world", file=sys.stdout)
if __name__ == "__main__":
    main()

Is it possible to have the shebang be a variable that is determined from the current conda environment?

For example:

From py3_env environment:

#!~/anaconda/envs/py3_env/bin/python

and from py3_clone_env environment:

#!~/anaconda/envs/py3_clone_env/bin/python

回答1:


I guess what you need is #!/usr/bin/env python:

#!/usr/bin/env python
import sys
print(sys.executable)

In this case, python is the python based on current PATH environment variables. So it is your current virtualenv's python.



来源:https://stackoverflow.com/questions/52087621/how-to-use-path-of-current-conda-environments-python-as-shebang-for-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!