如何将当前conda环境的python路径用作脚本脚本? [英] How to use path of current conda environment's python as shebang for a script?

查看:1104
本文介绍了如何将当前conda环境的python路径用作脚本脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有2个conda环境:py3_envpy3_clone_env

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

如果您的script.py具有以下结构:

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()

是否可以使shebang成为根据当前conda环境确定的变量?

例如:

py3_env环境中:

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

py3_clone_env环境中的

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

推荐答案

我想您需要的是#!/usr/bin/env python:

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

在这种情况下,python是基于当前PATH环境变量的python.因此,这是您当前的virtualenv的python.

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

这篇关于如何将当前conda环境的python路径用作脚本脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆