0

A script at /foo/bar.py tries to run a second script /main.py using the subprocess module. Although main.py runs fine with python main.py in the Windows command prompt, running bar.py which calls main.py causes an error

ConfigParser.NoSectionError: No section: 'user'

Why is there now a problem with the path to settings.ini, and how can we fix it?

~/settings.ini

[user]
id: helloworld

~/foo/bar.py

subprocess.Popen([sys.executable, "../main.py"])

~/main.py

Config = ConfigParser.ConfigParser()
Config.read("settings.ini")
userId = Config.get('user', 'id')
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • Is the current directory the same in each case? I ask because the filename `settings.ini` is not qualified with a directory name, so you might be picking-up two different files. – cdarke Jun 16 '16 at 22:07
  • it is more flexible to import the module and use its functions, classes directly instead of running it as a subprocess. See [Call python script with input with in a python script using subprocess](http://stackoverflow.com/q/30076185/4279) – jfs Jun 18 '16 at 22:58

2 Answers2

1

If settings.ini is presumed to be in the same directory as main.py you can deduce its full path from __file__ and read the settings using the full path.

main.py:

import os
ini_path = os.path.join(os.path.dirname(__file__), "settings.ini")
Config = ConfigParser.ConfigParser()
Config.read(ini_path)
robyschek
  • 1,995
  • 14
  • 19
0

Check that the read() method returns a non-empty list otherwise it means that settings.ini is not found. Relative paths are resolved relative to the current working directory (place where you've run python), not script's directory (where bar.py is stored).

You should probably use appdirs.user_config_dir(), to get the directory where to put user's configuration files. Different OSes such as Windows, macOS, Linux distributions may use different defaults. appdirs follows conventions that are appropriate for a given OS.

If you want to get data stored in a file that is packaged with your installed Python modules then you could use pkgutil.get_data() or setuptools' pkg_resources.resource_string() that work even if your package is inside an archive. It is not recommended to construct the paths manually but if you need the directory with the current script then you could put get_script_dir() function into your scriptβ€”it is more general than os.path.dirname(os.path.abspath(__file__)). If the relative position is always the same then you could use:

#!/usr/bin/env python
import os
import subprocess
import sys

main_script_dir = os.path.join(get_script_dir(), os.pardir)
subprocess.check_call([sys.executable, '-m', 'main'], cwd=main_script_dir)

See Python : How to access file from different directory

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670