• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add directory to sys.path??

 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Everybody's invited. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic