python – How to use ConfigParser with virtualenv? – Education Career Blog

I wrote a tool that looks in several places for an INI config file: in /usr/share, /usr/local/share, ~/.local/share, and in the current directory.

c = ConfigParser.RawConfigParser()
filenames = '/usr/share/myconfig.conf',
parsed_names =
for name in parsed_names:
    print 'using configuration file: ' + name

I have started using virtualenv and now my script installs myconfig.conf into /path/to/virtual/env/share/. How can I add this path to the list of paths searched by ConfigParser when the path to the virtualenv will be different each time? Also, if I installed to a virtualenv, should I still search the system /usr/share and /usr/local/share directories?


You should be able to get the venv share path with

os.path.join(sys.prefix, 'share', 'myconfig.conf')

Including /usr/share or /usr/local/share would depend on your application and if multiple installations by different users would be more likely to benefit or be harmed by global machine settings. Using the above code would include '/usr/share/myconfig.conf' when using the system python so it is probably safer to not include it explicitly.

Leave a Comment