How is the python module search path determined on Mac OS X?

2024/10/7 18:31:04

When a non built-in module is imported, the interpreter searches in the locations given by sys.path. sys.path is initialized from these locations (http://docs.python.org/library/sys.html#sys.path):

  1. the directory containing the input script (or the current directory)
  2. PYTHONPATH
  3. the installation-dependent default

While the first two sources are straight-forward, can anyone explain how the third one works, and what possibilities there are for influencing it?

Although I would be interested in a general solution, my specific issues are:

  • I have installed the Enthought distribution 7.2 32-bit, and then Scipy-Superpack. Now enthought python tries to import numpy from /Library/Python/2.7/, which is where superpack installed them, instead of from the enthought site-packages.
  • a wxPython application created with py2app -A does not have the same sys.path as when starting the application with python start_app.py.
Answer

The basis of the third source is set at compile time (or configure time, more precisely), depending on the platform. It is then expanded and added to at run-time via .pth files, etc. (which is something you can do once the Python executable is compiled to influence that third source).

This page of the Python documentation has all the information on how .pth files work, and also more information on how sys.path is constructed from build-time settings, etc. http://docs.python.org/library/site.html

I'm not sure why you want to influence that third source specifically though, when you can influence the whole sys.path. Anyhow, the three ways of influencing sys.path (without recompiling Python or patching the source code) are:

  1. Via the PYTHONPATH environment variable.
  2. By creating .pth files and dropping them where Python scans for packages. (See the link earlier for details.)
  3. Programmatically, by importing sys and then appending or prepending to sys.path

    import sys
    sys.path.insert(0, '/this/path/will/be/considered/first')
    

Hopefully one of these three ways should help you do what you want.

https://en.xdnf.cn/q/70209.html

Related Q&A

Apply Mask Array 2d to 3d

I want to apply a mask of 2 dimensions (an NxM array) to a 3 dimensional array (a KxNxM array). How can I do this?2d = lat x lon 3d = time x lat x lonimport numpy as npa = np.array([[[ 0, 1, 2],[ 3,…

Server Side Google Markers Clustering - Python/Django

After experimenting with client side approach to clustering large numbers of Google markers I decided that it wont be possible for my project (social network with 28,000+ users).Are there any examples …

Tensorflow numpy image reshape [grayscale images]

I am trying to execute the Tensorflow "object_detection_tutorial.py" in jupyter notebook, with my trained neural network data but it throws a ValueError. The file mentioned above is part of S…

Merge numpy arrays returned from loop

I have a loop that generates numpy arrays:for x in range(0, 1000):myArray = myFunction(x)The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensio…

Play mp3 using Python, PyQt, and Phonon

I been trying all day to figure out the Qts Phonon library with Python. My long term goal is to see if I could get it to play a mms:// stream, but since I cant find an implementation of this done anywh…

Python dictionary keys(which are class objects) comparison with multiple comparer

I am using custom objects as keys in python dictionary. These objects has some default hash and eq methods defined which are being used in default comparison But in some function i need to use a diffe…

How can I make np.save work for an ndarray subclass?

I want to be able to save my array subclass to a npy file, and recover the result later.Something like:>>> class MyArray(np.ndarray): pass >>> data = MyArray(np.arange(10)) >>&g…

With ResNet50 the validation accuracy and loss is not changing

I am trying to do image recognition with ResNet50 in Python (keras). I tried to do the same task with VGG16, and I got some results like these (which seem okay to me): resultsVGG16 . The training and v…

string has incorrect type (expected str, got spacy.tokens.doc.Doc)

I have a dataframe:train_review = train[review] train_reviewIt looks like:0 With all this stuff going down at the moment w... 1 \The Classic War of the Worlds\" by Timothy Hi... 2 T…

custom URLs using django rest framework

I am trying to use the django rest framework to expose my models as APIs.serializersclass UserSerializer(serializers.HyperlinkedModelSerializer):class Meta:model = Userviewsetclass UserViewSet(viewsets…