6

The Goal: Access / Write to the same temp files when using a common utility function called from various python modules.

Background: I am using the python Unittest module to run sets of custom tests that interface with instrumentation via pySerial. Because I am using the unittest module, I am unable to pass required variables, such as which serial port to use, into the unittest's test case. To get around this I am wanting to create a module that stores and returns pickled data. I have run into the issue that when I call the function get_foo() from test_case_1(), it tries to load the pickled data from the relative path based on test_case_1(), not the actual module that contains get_foo().

It is worth noting that I have contemplated using global variables, but there is a handful of data that I want to retain from run to run. Meaning that all python modules will be closed and I want to re-load the data that was stored on the previous execution.

I in SO question: Python - how to refer to relative paths of resources when working with code repository, I thought I found the solution in the first answer. To my dismay, this is not working for me in Python 2.7 (Debian)

Is there an reliable way to return the path to a specific file when called from different modules?

Community
  • 1
  • 1
Adam Lewis
  • 7,017
  • 7
  • 44
  • 62

1 Answers1

5

Probably you know this, but here the basics first:

## file one: main.py, main program in your working directory
# this code must run directly, not inside IDLE to get right directory name
import os, mytest
curdir=os.path.dirname(__file__) 
print '-'*10,'program','-'*10
print 'Program in',curdir
print 'Module is in', mytest.curdir
print 'Config contents in module directory:\n',mytest.config()
input('Push Enter')

The module

## file two: mytest.py, module somewhere in PATH or PYTHONPATH
import os
curdir= os.path.dirname(__file__)

print "Test module directory is "+curdir

## function, not call to function
config=open(os.path.join(curdir,'mycfg.cfg')).read
""" Example output:
Test module directory is D:\Python Projects
---------- program ----------
Program in D:\test
Module is in D:\Python Projects
Config contents in module directory:
[SECTIONTITLE]
SETTING=12

Push Enter
""""
Tony Veijalainen
  • 5,447
  • 23
  • 31
  • Thank you for going into detail in your example. It was what I needed to figure out my troubles. Turns out that I was using the dirname incorrectly in my tests. Can you point to documentation where the dirname(__file__) is described? I did not see it on python.org. – Adam Lewis Mar 28 '11 at 20:31
  • Documentation can be found in http://docs.python.org/library/os.path.html in unlikely case that you have not help file locally. – Tony Veijalainen Mar 28 '11 at 21:16
  • This is actually the page I scoured trying to find documentation on the __file__ field of the dirname function. Any ideas? – Adam Lewis Mar 29 '11 at 05:12
  • You mean http://docs.python.org/release/2.6.6/reference/simple_stmts.html#index-1058 – Tony Veijalainen Mar 29 '11 at 16:49