9

I just installed the py27-numpy package via MacPorts and python will not find the module when I use this command: import scipy

I used the help('modules') command and the scipy port did not come up.

Clearly the path is not configured correctly or MacPorts is not installing in the correct place, but either way, it would solve my problem to know where this package is being installed.

Where can I find the path to MacPorts-installed package, py27-scipy?

Output of echo $PATH command:

/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:directory/bin

I cannot find the package in any of those locations.

Thanks for the help.

Andrew
  • 3,901
  • 15
  • 50
  • 64
  • The `PATH` environment variable is simply for your shell (probably `bash(1)`) to find programs to execute without a qualified path name: `cat` rather than `/bin/cat`, `ftp` rather than `/usr/bin/ftp`, etc. (It is also used by the `execlp(3)` and `execvp(3)` functions, but that use is less frequent than the shell.) It's got nothing to do with Python module search path, `PYTHONPATH`. See `python(1)` for more information on `PYTHONPATH` and `bash(1)` for information on `PATH`. – sarnold May 27 '11 at 02:00

6 Answers6

15

To find the location of installed components, use the contents subcommand:

port contents py27-numpy

As for getting python to find the package, see @fardjad's response.

Community
  • 1
  • 1
Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • 1
    Note that everything (except a symlink for f2py) in the py27-numpy package is always installed in the MacPorts python2.7 instance. So, regardless of where py27-numpy is installed, the key is knowing how to invoke the MacPorts python2.7. – Ned Deily May 27 '11 at 02:22
  • Your answer was also very helpful and I would check two if I could. – Andrew May 27 '11 at 03:07
13

Your PATH is incorrect. It appears to be picking up another Python 2.7, likely one installed using a binary installer from python.org or elsewhere, and not the MacPorts installed one. Try removing the the /Library/Frameworks/Python.framework/Versions/2.7/bin from PATH or just invoke the MacPorts Python directly:

/opt/local/bin/python2.7
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 2
    You can also use `python_select` to set the default `python` or you can invoke this command: `sudo port select --set python python27`. – fardjad May 27 '11 at 02:14
  • 1
    The MacPorts `python_select` sets a symlink for `python` in /opt/local/bin so it won't help if `/Library/Frameworks/Python.framework/Versions/2.7/bin` comes first on PATH. – Ned Deily May 27 '11 at 02:15
  • 1
    Ahhh yes, didn't note the OP's `PATH` output. – fardjad May 27 '11 at 02:17
3

MacPorts should install Python packages in /opt/local/Library/Frameworks/Python.framework/2.7/site-packages by default. So make sure to set $PYTHONPATH environment variable in your .profile file:

export PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/site-packages"
fardjad
  • 20,031
  • 6
  • 53
  • 68
  • 5
    That's not where MacPorts installs things. – Ned Deily May 27 '11 at 02:02
  • @Ned Deily it does so in my machine :) – fardjad May 27 '11 at 02:05
  • I can't remember if I changed my `MacPorts` installation configuration but it won't be a problem as the OP can find the location using `contents` (Jeremy W. Sherman response). – fardjad May 27 '11 at 02:08
  • Then you changed its defaults or you are setting a configuration variable via an environment variable or something like .pydistutils.cfg. MacPorts Pythons are framework builds and the default site-packages directory is in `/opt/local/Library/Frameworks/Python.framework/...` – Ned Deily May 27 '11 at 02:09
  • But the issue is undoubtedly that the OP is using the wrong Python. The MacPorts py27-scipy package causes MacPorts to install its own Python and that's not located in /Library/Frameworks/. – Ned Deily May 27 '11 at 02:09
  • How do we just fix MacPorts to not suck and install packages in, you know, the freaking directory that comes with the computer and is designated for such things (/Library/Python/2.7/site-packages)? – ArtOfWarfare Nov 11 '13 at 16:15
  • @ArtOfWarfare **MacPorts** does this to prevent conflicts. There's nothing wrong with it IMO. – fardjad Nov 11 '13 at 17:57
  • @fardjad - Hiding the file may prevent conflicts, but it also significantly inhibits the ability of anything to work together (not to mention generate a huge amount of frustration and confusion in the process.) I've decided to uninstall MacPorts (again) - it's just not worth the effort to make it play nice with anything. It always insists on hiding all of its files in the most retarded directories where nothing else can find them (nor can I, without a huge amount of effort on my part.) – ArtOfWarfare Nov 11 '13 at 20:25
1
sudo port select --set python python27

is the best answer to install port's python system-wide

Atento
  • 786
  • 6
  • 7
0

With Homebrew only using the latest, the Mac system version, and MacPorts for the others in-between, I was confused until I found python locations differ depending on the installer.

Here's an opinionated tip: Use virtualenvs for your projects and don't change your default version with the MacPorts. I won't and don't want to remember to updoot my python in the middle of something so I rely on virtualenvs. Choose and find the python version on the computer, then mkvirtualenv --python=/found/u/python3.X getawesome.

NathanQ
  • 1,024
  • 18
  • 20
0

Based on Jeremy W. Sherman's answer

I checked my python version

python --version
Python 3.8.5

and location:

which python
/opt/local/bin/python

and then tried:

sudo port contents python38 

which lists 7285 lines:

Port python38 contains:
  /Applications/MacPorts/Python 3.8/IDLE.app/Contents/Info.plist
  /Applications/MacPorts/Python 3.8/IDLE.app/Contents/MacOS/IDLE
  /Applications/MacPorts/Python 3.8/IDLE.app/Contents/MacOS/Python
  /Applications/MacPorts/Python 3.8/IDLE.app/Contents/PkgInfo
  ...
  /opt/local/share/man/man1/python3.8.1

combining that with fardjad's answer leads to:

sudo port contents python38 | grep site-packages

with the output:

/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/README.txt

since we need the directory modifying the command to:

dirname $(sudo port contents python38 | grep site-packages)

gives the desired directory:

/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

so it's possible to end up with the one-liner:

One-Line PYTHONPATH setting in macports:

 export PYTHONPATH=$(dirname $(sudo port contents python38 | grep site-packages))

and we can check the result:

echo $PYTHONPATH
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

For reference see how the Eclipse Liclipse python IDE dialog for setting the PATH looks - there are some more directories you might want to include for a fully specified PYTHONPATH.

Liclipse dialog for Python PATH selection

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186