简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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