Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I run some python scripts on a server, they are under a cvs repository and, for various reasons use absolute path names.

I'm trying to use eclipse as an editor - I check the projects out, but when running them I find that the files are not where the code expects them. ('/home/staff/root/analysis/test.txt' rather than '../test.txt' for example)

Is there a way to get eclipse to trick python into thinking there is a different directory structure? If I did a global find/replace of '/home/staff/root/analysis/' for '..' that would do the trick, but it doesn't appear very guru-like...

any ideas?

share|improve this question

2 Answers 2

If you want to change hard coded relative paths into absolute ones, you can always edit the text with any editor (sed) but I doubt that's what you really required.

Probably the problem is that you are not located on the directory the tool expects you to be when running something, a simple os.chdir should allow you to change that.

If not follow Reece advice.

share|improve this answer

I don't have an answer for your question in the narrowest sense. I do have two suggestions that may achieve the same end.

Suggestion 1: Symlink

If the file dependencies are rooted in a common tree (or a small number of trees), you might be able to create a symlink to the appropriate files. So, let's say that you want /home/staff/root/analysis/test.txt to resolve to /path/to/working/directory/test.txt, then you could do this:

$ sudo ln -fns /path/to/working/directory /home/staff/root/analysis 

This requires root privileges. The effect would be that the entire path prefix /home/staff/root/analysis would resolve to /path/to/working/directory. This applies to files immediately under the prefix as well as all subdirectories.

Suggestion 2: Support configuration options for runtime dependencies

If files are really required for runtime or testing, they should be part of the package. Describing exactly how to do this is beyond the scope of this answer, but here are some resources, which you might already be well aware:

  • Expert Python Programming http://goo.gl/YoDx6
  • Use SafeConfigParser to parse conf/ini files (e.g.,data-dir=/home/staff/root/analysis)
  • Use argparse to get options from the command line/override conf (e.g., --data-dir=..)
share|improve this answer
    
The OP doesn't detail what the "various reasons" are for the hardcoded paths, but getting rid of them (Suggestion 2) is clearly the better way to go IMHO. –  martineau Sep 5 '13 at 18:46

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.