0

The import statements executed from the pth files seem to execute fine. But I don't seem to be able to access any of the modules that are imported in this way.

What is this sorcery?

5
  • 3
    what is the curse blocking you from adventuring further in this dangerous quest? (or, for the records: what's the faulty code? what's the error you're having?) Commented Dec 12, 2012 at 14:49
  • @Samuele As I said, the modules imported in the pth files do not become available in the code. Commented Dec 12, 2012 at 14:59
  • 2
    please, elaborate: "don't seem to be able to access". What is the full traceback? .pth-files might change what modules you can import but to access a module you need to import it in your code explicitly. To affect anything, .pth-files should be in directories recognized by site.py (site.getsitepackages(), site.getusersitepackages(), site.addsitedir()). Commented Dec 12, 2012 at 15:03
  • pth files are only links to other python-sources. they are no source. Commented Dec 12, 2012 at 15:04
  • @Sebastian Well, pth files support the import statement. What is that for then, if the modules imported using the statement are not available to me in the code. There is no traceback. Just the names from the module remain undefined. Why do I have to explicitly import them again? I am OK with doing that, BTW. Just want to understand the idea behind the import statements in the pth files. Commented Dec 12, 2012 at 15:17

1 Answer 1

3

From your comments I'm guessing that you're getting NameError exceptions for some modules due to confusion about the purpose of .pth-files.

pth stands for path. The purpose is to add paths to sys.path (pythonpath - path that Python uses to find modules during import). See site module documentation.

The lines that start with import can contain any code but generally they modify sys.path. For example, setuptools machinery:

import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

Another common case is when .pth-files are used to implement "namespace" packages:

import sys,types,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('zope',)); ie = os.path.exists(os.path.join(p,'__init__.py')); m = not ie and sys.modules.setdefault('zope',types.ModuleType('zope')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)

.pth-files are not there to make module names appear in your code. To import some_module, add at the top of each module where you want to use it:

import some_module
Sign up to request clarification or add additional context in comments.

6 Comments

That is what I am trying to get some insight on. You can already add to the path by adding the corresponding lines to the pth file. Then what is the purpose / thought behind allowing the import statement? What problem is it intended to solve? What use case does it cater to?
@Ziffusion: I've added use cases for import inside .pth-files
@Ziffusion The problem it solves is that you can then use that module in your code.
@Marcin Not sure if you read the question :) Because that is exactly what I am saying does NOT happen. You cannot use the modules imported in pth files in your code. Have you been able to do this?
@Ziffusion I read the question. It doesn't actually have any code or anything else to demonstrate your problem. The point of the import is that you can use the module once you have imported it. You cannot use it before you have imported it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.