Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

How to add directory to sys.path??

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Hello,

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: -

A--
--help.py
B--
--two.py

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

I have found one way to do this by adding following lines to two.py: -



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
    
    1

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: - abc.py in two different directories..
And I have added both the directories in the PYTHONPATH.

Now, if I write: -
in one my module..
Which abc.py 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??
1). PYTHONPATH, or
2). somfile.pth in site-packages directory??
 
I agree. Here's the link: http://aspose.com/file-tools
 
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?