global variable not seen (bug?)

Bengt Richter bokr at oz.net
Wed Jan 8 15:50:14 EST 2003


On Wed, 8 Jan 2003 18:56:15 +0100, Michal Vitecek <fuf at mageo.cz> wrote:
[...]
>
> what i wanted was an easy solution to be able to access (read/write,
> etc.) data files in my python scripts. the data files are located in
> some fixed directory but the scripts (with classes that operate on
> them) can be whatever deep in directories and be imported and used from
> scripts that can be again whatever deep. some example:
>
>    projectRootDirectory:
>        importFileModuleToWorkOnFile.py*
>        
>    projectRootDirectory/dataFiles:
>        File.dat
>
>    projectRootDirectory/classes:
>        FileModuleWorkingOnFile.py
>
>    projectRootDirectory/tests:
>        testFileModuleWorkingOnFile.py*
>
> where * denotes executable python script and module
> FileModuleWorkingOnFile.py contains a class that wants to be able to
> access file File.dat no matter what is the location of executable
> script importing it. if real globals were supported in python i could
> have the scripts roughly as follows:

I'm not getting a clear picture of your overall design, but "sys.path tweaking"
sounds like you ought to look for another way to do whatever got you to
consider that.

>
> importFileModuleToWorkOnFile.py:
     #XXX# PROJECT_ROOT_DIRECTORY = "."
     import MichalVitecekGlobals
     MichalVitecekGlobals.PROJECT_ROOT_DIRECTORY = "."
                                                    ^---[Note 1]

>    # sys.path tweaking
     ^^^^^^^^^^^^^^^^^^^ ?? why ??
>    import FileModuleWorkingOnFile
>
>    # make an instance of class ClassWorkingOnFile() and call its
>    # methods to do some work on file File.dat
>    ...
>
>
> testFileModuleWorkingOnFile.py:
     #XXX# PROJECT_ROOT_DIRECTORY = ".."
     import MichalVitecekGlobals
     MichalVitecekGlobals.PROJECT_ROOT_DIRECTORY = ".."
                                                    ^---[Note 1]

>    # sys.path tweaking
     ^^^^^^^^^^^^^^^^^^^ ?? why ??

>    import FileModuleWorkingOnFile
>
>    # make an instance of class ClassWorkingOnFile() and call its
>    # methods to do some work on file File.dat
>    ...
>
>
> FileModuleWorkingOnFile.py:
>    class ClassWorkingOnFile(object):
>        ...
>
>        def workOnFile(self):
             #XXX# global PROJECT_ROOT_DIRECTORY
             import MichalVitecekGlobals
>            
             #XXX# f = open(PROJECT_ROOT_DIRECTORY + "/dataFiles/File.dat")
             f = open(MichalVitecekGlobals.PROJECT_ROOT_DIRECTORY + "/dataFiles/File.dat")
>            ...


The above might do what you want, if you put MichalVitecekGlobals.py somewhere in the
search path, e.g. D:\python22\lib\site-packages\MVProject\MichalVitecekGlobal.py or
wherever makes sense for your setup.

By importing the same module, it becomes a namespace that you can share like a "real global"
namespace using attribute (dotted) names to access its named content.

Some kinds of global sharing make sense, but if you find you're using it as a scratch pad
to pass info between things, your design probably needs a rethink. I'm a little suspicious ;-)

+--- [Note 1] -----------------------------------------------------------------------------
+ '.' and '..' in file paths are not relative to the directories of files that
+ contain that text, even if they happen to be scripts. They will be relative to the current
+ working directory of whatever process is running (which you can determine with os.getcwd()
+ and change with os.chdir('/some/new/path')). It will be your interactive current working
+ directory if you start python that way, but if the interpreter is started by a server daemon
+ etc., it will depend on how that's done.  Type import os; help(os.path) for more info.
+-------------------------------------------------------------------------------------------

Regards,
Bengt Richter




More information about the Python-list mailing list