从脚本

时间:2019-08-05 15:16:07

标签: python runpy

我有一个小的python文件c:\tmp\server_config,其定义如下的字典:

conf = {'server1':'11.27.32.99',
        'server2':'11.27.44.42'}

我希望从另一个脚本中访问该词典。我可以从命令行执行以下命令:

>>> from runpy import run_path
>>> settings = run_path("c:\\tmp\\server_config.py")
>>> settings['conf']
{'server1': '11.27.32.99', 'server2': '11.27.44.42'}

但是,当我尝试从另一个脚本中执行settings = run_path("c:\\tmp\\server_config.py")时,会得到以下回溯:

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "C:/Users/sjo/AppData/Roaming/QGIS/QGIS3/startup.py", line 54, in 
settings = run_path('c:\\tmp\\server_config.py')
  File "C:\OSGEO4~1\apps\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
  File "C:\OSGEO4~1\apps\Python37\lib\runpy.py", line 93, in _run_module_code
with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname):
  File "C:\OSGEO4~1\apps\Python37\lib\runpy.py", line 54, in __enter__
self._saved_value = sys.argv[0]
AttributeError: module 'sys' has no attribute 'argv'

我在做什么错(Python 3.7,Windows10)?

0 个答案:

没有答案