This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Jython/Python and the fly likes How to add directory to sys.path?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "How to add directory to sys.path??" Watch "How to add directory to sys.path??" New topic

How to add directory to sys.path??

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370


I have just started programming in Python, and I ran into a problem

I want to import a module which is not in the same directory as my current module...
Directory structure is like this: -


Now, and are in two parallel directory... I want to use in

I have found one way to do this by adding following lines to -

But I don't want to do this in every python module I make..

I python documentation I read that, sys.path is " Initialized from the environment variable PYTHONPATH, plus an installation-dependent default."

Now, as I get from this statement.. PYTHONPATH is an environment variable set to the path where my module is stored..
But, even when I have set this environment variable, I cannot get it work..

Can anyone tell, how I can set the variable.. (Like we set classpath in Java)??
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370

Solved this problem..

I didn't knew that we need to restart the interpreter if we set the environment variable : PYTHONPATH
Now this looks something not so tidy..

Suppose, if we have two python modules with same name: - in two different directories..
And I have added both the directories in the PYTHONPATH.

Now, if I write: -
in one my module..
Which will get imported??

Also, apart from the above solution (Setting PYHTONPATH environment variable), I have also found one more way to set directory..
This is by creating a text file with extension (.pth) containing all the directories, and put it in /site-packages directory..

Want to ask, which one would be a better way??
2). somfile.pth in site-packages directory??
I agree. Here's the link:
subject: How to add directory to sys.path??
Similar Threads
JRE problem.
getting and setting environment variables
Remote Debugging the Oracle Weblogic 10.3.3
Properties.def not found
Why JDK installation does not adjust path?