<p>对于这类问题,您有多种解决方案:</p>
<p><strong>在每台机器上创建环境变量,在脚本中执行以下操作:</strong></p>
<pre><code>import os
student_path = os.environ['STUDENT_PATH']
faculty_path = os.environ['FACULTY_PATH']
print(student_path, faculty_path)
</code></pre>
<blockquote>
<p>Personal opinion : I don't like to configure my scripts using environment variables as the ones you chose may be used by another software + it is always messy to debug</p>
</blockquote>
<hr/>
<p><strong>使用<a href="https://docs.python.org/3.7/howto/argparse.html" rel="nofollow noreferrer">arguments</a></strong></p>
<pre><code>import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", " student")
parser.add_argument("-f", " faculty")
args = parser.parse_args()
student_path = args.student
faculty_path = args.faculty
print(student_path, faculty_path)
</code></pre>
<p>然后像这样调用脚本,并根据机器调整这行</p>
<pre><code>python <yourscript> -s <student_path> -f <faculty_path>
</code></pre>
<blockquote>
<p>Personal opinion : I use arguments when I want to control a small amount of parameters on my scripts to change its behavior (verbose, nb of cpus, ...). </p>
</blockquote>
<hr/>
<p><strong>创建配置文件并使用<a href="https://docs.python.org/3/library/configparser.html" rel="nofollow noreferrer">configparser</a></strong></p>
<p>你知道吗配置.ini文件</p>
<pre><code>[Paths]
student_path=<path_on_machine>
faculty_path=<path_on_machine>
</code></pre>
<p>脚本用法:</p>
<pre><code>import configparser
config = configparser.ConfigParser()
config.read('config.ini')
student_path = config.get('Paths', 'student_path')
faculty_path = config.get('Paths', 'faculty_path')
print(student_path, faculty_path)
</code></pre>
<p>然后在每台机器上部署不同的<code>config.ini</code>文件(像<a href="https://www.ansible.com/" rel="nofollow noreferrer">ansible</a>这样的工具可以帮助您实现自动化)</p>
<blockquote>
<p>Personal opinion : I use config files when I need to adapt parameters when deploying on new machines. I don't like to use arguments for this purpose as I don't want to specify the same values every time I use my script (often these kind of parameters don't have good default values).</p>
</blockquote>
<hr/>
<p><strong>创建模块</p>
<p>您还可以创建一个模块来存储这些参数,而不是一个配置文件。你知道吗</p>
<p>我的_配置.py你知道吗</p>
<pre><code>student_path="<path_on_machine>"
faculty_path="<path_on_machine>"
</code></pre>
<p>然后导入</p>
<p>你知道吗脚本.py你知道吗</p>
<pre><code>import my_config
print(my_config.student_path, my_config.faculty_path)
</code></pre>
<blockquote>
<p>I don't have any personal opinion on config files vs config modules. Read <a href="https://stackoverflow.com/questions/4208323/what-is-the-benefit-of-using-the-configparser-instead-of-a-regular-python-py-fil">this</a> if you want some elements of comparison. </p>
</blockquote>