0

I have a project that exists at a specific location:

~/repos/my_proj

And lets say it has a structure like the following:

my_proj/
   stuff1
   stuff2

And if I'm that directory I can include python code as one would think

from stuff1 import func1
from stuff2 import func2

Is there a way to "find" that directory and use it as a variable so I can include that code anywhere

Lets say I'm at ~/Docs/TestArea

I'd like to be able to do

from my_proj_dir.stuff import func1

Is there any way to do that?

sedavidw
  • 11,116
  • 13
  • 61
  • 95

1 Answers1

0

There's a few ways to approach this problem. The better one is to set your PYTHONPATH environment variable to include the folder ~/repos/my_proj.

You could also from within Python you can manipulate the sys.path list that contains all the search paths where Python will be looking for modules -- before any import, you could just write

import sys
sys.path.append('~/repos/my_proj')

This is a bit uglier than setting PYTHONPATH because when you move your code to a different computer you would need to replicate the same folder structure. However it allows the code to decide what paths to search after the program has already started, which can be useful sometimes.

Kevin S
  • 898
  • 1
  • 9
  • 13