How do I find the name of the conda environment in which my code is running?

后端 未结 8 1668
耶瑟儿~
耶瑟儿~ 2020-12-08 18:48

I\'m looking for a good way to figure out the name of the conda environment I\'m in from within running code or an interactive python instance.

The use-case is that

相关标签:
8条回答
  • 2020-12-08 19:42

    very simply, you could do

    envs = subprocess.check_output('conda env list').splitlines()
    active_env = list(filter(lambda s: '*' in str(s), envs))[0]
    env_name = str(active_env).split()[0]
    
    0 讨论(0)
  • 2020-12-08 19:45

    On Windows (might work but untested on Linux):

    import sys
    import os
    
    # e.g. c:\Users\dogbert\Anaconda3\envs\myenvironment
    print( sys.exec_prefix.split(os.sep)[-1] )
    

    Answers using environment variables or assuming the path separator is "/" didn't work in my Windows/Anaconda3 environment.

    This assumes you are in an environment.

    0 讨论(0)
提交回复
热议问题