How to use export with Python on Linux

后端 未结 12 1523
梦如初夏
梦如初夏 2020-11-29 23:09

I need to make an export like this in Python :

# export MY_DATA=\"my_export\"

I\'ve tried to do :

# -*- python-mode -*-
# -         


        
12条回答
  •  遥遥无期
    2020-11-29 23:37

    One line solution:

    eval `python -c 'import sysconfig;print("python_include_path={0}".format(sysconfig.get_path("include")))'`
    echo $python_include_path  # prints /home//anaconda3/include/python3.6m" in my case
    

    Breakdown:

    Python call

    python -c 'import sysconfig;print("python_include_path={0}".format(sysconfig.get_path("include")))'
    

    It's launching a python script that

    1. imports sysconfig
    2. gets the python include path corresponding to this python binary (use "which python" to see which one is being used)
    3. prints the script "python_include_path={0}" with {0} being the path from 2

    Eval call

    eval `python -c 'import sysconfig;print("python_include_path={0}".format(sysconfig.get_path("include")))'`
    

    It's executing in the current bash instance the output from the python script. In my case, its executing:

    python_include_path=/home//anaconda3/include/python3.6m
    

    In other words, it's setting the environment variable "python_include_path" with that path for this shell instance.

    Inspired by: http://blog.tintoy.io/2017/06/exporting-environment-variables-from-python-to-bash/

提交回复
热议问题