Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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? Thanks,

share|improve this question
add comment

3 Answers 3

up vote 10 down vote accepted

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.

share|improve this answer
    
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
2  
Isn't the user just asking how to import? Answer to that below. –  sgillies Mar 29 '11 at 21:20
show 1 more comment

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.

share|improve this answer
    
+1 - definitely the simplest way to go –  geographika Mar 30 '11 at 12:09
add comment

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

share|improve this answer
    
Thanks geographika! I'll have a look at that. –  User Error Mar 29 '11 at 19:26
    
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.