简体   繁体   English

创建新脚本时,是否可以“自动加载” python变量?

[英]Is there a way to 'auto-load' python variables when you create a new script?

I have a set of variables that I am using for many different projects. 我有一组用于许多不同项目的变量。 Is there some sort of config file, or any similar options, that will automatically load a set of pre-defined variables when you make/open a new script? 是否有某种配置文件或任何类似的选项,当您创建/打开新脚本时会自动加载一组预定义变量?

For example, if I have: 例如,如果我有:

x = "hello"
y = 55

Then I'd like those variables to be automatically defined. 然后,我希望这些变量是自动定义的。 I know that the .R profile does something similar in RStudio, but I'm looking for this option in Python. 我知道.R配置文件在RStudio中具有类似功能,但是我正在Python中寻找该选项。 I'm using Spyder IDE, if that helps. 如果有帮助,我正在使用Spyder IDE。

Python does not have any concept of rc files. Python没有rc文件的任何概念。

If you want to do it in the Python level, you can use the import mechanism to declare the variables in a module and import that on each file that needs. 如果要在Python级别上执行此操作,则可以使用import机制在模块中声明变量,然后将其导入每个需要的文件中。

If you want to do it at the system level, you can create environment variables in your ~/.bashrc for example like: 如果要在系统级别执行此操作,则可以在~/.bashrc创建环境变量,例如:

export PY_X='hello'
export PY_Y=55

Make sure the variables are not overriden later, you can make them more verbose in that case to avoid name clashes. 确保以后不重写变量,在这种情况下,可以使它们更加冗长,以避免名称冲突。

Then in your Python files, you can can access them from os.environ dict: 然后在您的Python文件中,您可以从os.environ dict访问它们:

x = os.environ.get('PY_X', None)
y = os.environ.get('PY_Y', None)

Otherwise, for having variables only for the python process, you can directly use them at the process start time: 否则,对于仅用于python进程的变量,您可以在进程启动时直接使用它们:

PY_X='hello' PY_Y=55 python ...

Retrieving the env vars are same as mentioned above. 检索环境变量与上面提到的相同。

I don't know if this is the best way, but it is a bash script I made a little while back for making new perl files (I changed it to be python for you). 我不知道这是否是最好的方法,但这是bash脚本,我花了一段时间才制作新的perl文件(我将其更改为python)。 Might be helpful depending on how many things you want to put in the file. 可能会有所帮助,具体取决于您要在文件中放入多少东西。

#!/bin/bash
# usage
if [ "$1" == "-h" -o -z "$1" ]; then
  echo "Usage: ~/.mkpython file1.py <file2.py>"
  echo "  No  file2.pl passed -> Creates empty file1.py and opens in vim insert mode"
  echo "  Yes file2.pl passed -> Copies file2.py into file1.py and opens vim insert"
  exit 0
fi

# prevents accidental erase of file
if [ -a $1 ]
  then
    read -p "replace file $1? " -n 1 -r
    echo
    if [[ ! $REPLY =~ [Yy]$ ]]
      then
        exit 1
    fi
fi

#copies file 2 into file 1
if [ -n "$2" -a -a $2 ]
  then
    cat $2 > $1
    chmod 0755 $1 && vim $1
    exit 0
fi

#if no file 2 then creates a basic file 1

echo 'Stuff you want in your file goes here' > $1
chmod 0755 $1 && vim -c 'startinsert' $1 +3

This may be of help to you. 可能对您有帮助。 It describes the IPython configuration system. 它描述了IPython配置系统。 The long and short of it is that you can put whatever you want in the ~/.ipython/profile_default/ipython_config.py file and it will run every time you fire up the IPython interpreter. 它的长短之处是您可以将所需的任何内容放入〜/ .ipython / profile_default / ipython_config.py文件,并且该文件将在每次启动IPython解释器时运行。 If you are using another interpretter then you will have to specify to get a better answer. 如果使用其他口译员,则必须指定以获得更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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