10

I want to set up a single python file containing variables that have the locations of all my data sources. This would then be used by all my other scripts, then as a data source changes, I only have to edit the one file.

My Data_sources.py would look something like this:

BC_BEC = "Database Connections\\BC.sde\\FOREST_BC.BC_Eco\\BC.BC_BEC"
BC_TFL = "Database Connections\\Forest_BC.sde\\BC.BC_Admin\\BC.TFL_Boundary"

and so on...

How do I get all my other scripts to load or access these variables in the Data_sources.py?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
User Error
  • 487
  • 1
  • 4
  • 16
  • Questions such as this, which are pure Python rather than about a Python module or site-package for GIS should normally be researched/asked at [so]. – PolyGeo Apr 19 '15 at 03:16

3 Answers3

12

In any script in the same directory as Data_sources.py, put

from Data_sources import BC_BEC, BC_TFL

at the top. This works because the working directory is always at the head of the python path unless you've modified it.

>>> import sys
>>> sys.path[0] == ''
True

See also: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html#d0e4550

If you want to share the data more widely, you can package it like others have suggested.

sgillies
  • 9,056
  • 1
  • 33
  • 41
10

You could create an installed Python module that is importable from every Python script you run by using Distutils to package it.

From the Python doc:

If all you want to do is distribute a module called foo, contained in a file foo.py, then your setup script can be as simple as this:

from distutils.core import setup
setup(name='foo',
      version='1.0',
      py_modules=['foo'],
      )

You can then install it via

python.exe setup.py install

And then you can import foo from any Python script, or you can use

python.exe setup.py bdist_wininst

To create a windows installer for the module so you can distribute it to other computers.

Jason Scheirer
  • 18,002
  • 2
  • 53
  • 72
  • there's a nice chapter in the 'Byte of Python' book showing examples of building using ang importing modules. – jonatr Mar 29 '11 at 18:55
  • I like this suggestion of creating a Module but I'm not very knowledgeable of them. The command "python.exe setup.py install" will use a pointer to foo.py, or will it use what's currently in foo.py? If I later change foo.py the module will still be current? Thanks. – User Error Mar 29 '11 at 19:05
  • I'll also look for that book. Thanks for the help Jason and jonatr. – User Error Mar 29 '11 at 19:08
  • Vocabulary correction: the more precise python word is "package" -- helpful for looking up documentation, and to be clear that packages and modules are two different things in the python world. Also, a suggestion/hint: If you do setup.py develop (instead of install), edits to the package will be picked up as soon as they are saved, without need for a reinstall. (This is probably appropriate for the original questioner's data_sources.py.) – Dan S. Mar 29 '11 at 19:10
  • 3
    Isn't the user just asking how to import? Answer to that below. – sgillies Mar 29 '11 at 21:20
8

Create a text config file and read it with Python's ConfigParser.

Slightly nicer examples than the official documentation at http://effbot.org/librarybook/configparser.htm

geographika
  • 14,320
  • 4
  • 53
  • 77
  • @usererror - a package is good for distributing code, but it doesn't really solve the issue of keeping your parameters in one place. – geographika Mar 29 '11 at 20:35