19

If I put a *.pth file in site-packages it's giving an ImportError. I'm not getting how to import by creating a *.pth file.

(Refers to importing in python)

2
  • You should add a link to the original question, stackoverflow.com/questions/697281/importing-in-python Commented Mar 31, 2009 at 7:29
  • 2
    -1: You should provide (1) code, (2) the actual error message, (3) the .pth file you're using. Commented Mar 31, 2009 at 12:41

2 Answers 2

44

If you put a .pth file in the site-packages directory containing a path, python searches this path for imports. So I have a sth.pth file there that simply contains:

K:\Source\Python\lib

In that directory there are some normal Python modules:

logger.py
fstools.py
...

This allows to directly import these modules from other scripts:

import logger

log = logger.Log()
...
Sign up to request clarification or add additional context in comments.

3 Comments

Short and simple... :-)
But where do you put the sth.pth file? when python is installed in C:\python27 ?
thank you, i have used import sys; sys.path.append(path) for some time now. this works
27
/tmp/$ mkdir test; cd test
/tmp/test/$ mkdir foo; mkdir bar
/tmp/test/$ echo -e "foo\nbar" > foobar.pth
/tmp/test/$ cd ..
/tmp/$ python
Python 2.6 (r26:66714, Feb  3 2009, 20:52:03)
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site, sys
>>> site.addsitedir('test')
>>> sys.path[-3:]
['/tmp/test', '/tmp/test/foo', '/tmp/test/bar']

1 Comment

please include a short description of what you're doing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.