3

Thank you in advance for the advice!

Background:

Relative to the people on this site I'm new to programming, and despite some research I'm not familiar with using Unix-type shells, with what managing packages really involves, or with anything which is done outside of the "Dr. Java" IDE or default R GUI. I have a late-2008 macbook with OSX 10.5.8

My Goal:

I'm trying to get the following packages installed in Python: scipy, numpy, matplotlib, networkX

My Story:

From what I understand my 10.5.8 comes installed with a Python 2.5, which the system needs for its functioning, and you really shouldn't mess with. The packages I installed require Python 2.7, so after reformatting my machine, I installed Python 2.7.2 for OSX 10.3 (the latest Python/OSX that was offered of 2.7.X) using the .dmg off of the Python website. Then, I installed XCode using the .dmg (appropriate version for my OS) off of the Apple site, and installed MacPorts (appropriate version for my OS) using the .dmg off of their site. Then, I believe I used MacPorts to install the packages:

sudo port install py27.numpy

However, when I try to import the packages I've apparently installed, Python tells me they don't exist. I'm pretty sure I have only 2 versions of Python on my machine, 2.5 for OSX and 2.7.2 for me (how do I check this?), and if I check which version of Python is running, whether through the Shell or through the IDLE, I get:

>>> import sys
>>> print sys.version
2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39) 
[GCC 4.0.1 (Apple Inc. build 5493)]

If I run "which python" i get:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

When I check where MacPorts installed the packages using

port contents py27-packagename

I get directories mostly in:

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

with a few directories in

/opt/local/share/py27-matplotlib/examples/
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/share/doc/networkx-1.5/

and one directory each in

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/f2py
/opt/local/share/doc/py27-matplotlib/CHANGELOG
/opt/local/share/doc/py27-matplotlib/README.txt
/opt/local/share/doc/py27-matplotlib/TODO

My Question:

How do I get Python to recognize my packages?? Ideally I'd like to set it up so I can use the Python IDLE, as well as invoking it from the command line.

I've tried replicating some of the solutions at: Locate MacPorts package? But I don't really understand what I'm doing all that well so It's difficult to adapt what's being done there.

Thank you again everyone!

Community
  • 1
  • 1
whearyou
  • 358
  • 1
  • 5
  • 18

1 Answers1

2

i initially misunderstood your question to be the standard one of - why can't i run my macports python? but in fact, you specified that you are trying to use the self-installed python, and you are. the problem is, that python doesn't know where to look for your libraries which macports installed.

you were on the right track with that question you linked to. see this response, but replace the path with your actual path to your macports libs, viz:

export PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH"

see if that doesn't do the trick!

Community
  • 1
  • 1
dlchet
  • 1,053
  • 6
  • 13
  • 1
    checkorbored, thanks so much for the help. I looked at the link you posted. Where can I find my .profile file? If I edit it, will the changes remain when I exit the terminal window, and will I be able to use them in IDLE? – whearyou Feb 16 '12 at 04:53
  • in fact, to test you don't even need to find that file. simply enter the command (beginning with export) at your command prompt. once that is done, then try your python stuff again. – dlchet Feb 16 '12 at 04:54
  • (your .profile file is what controls your shell experience, and is beyond the scope of this question - it depends on a lot of things.) – dlchet Feb 16 '12 at 04:55
  • Thanks checkorbored! I entered the text you indicated, and it worked!! But... when I restart the terminal, I'm back to square one. How do I make this change permentant? – whearyou Feb 16 '12 at 04:57
  • it's really a different question, but to answer it i need to know what shell you are using. what happens when you type 'echo $SHELL'? – dlchet Feb 16 '12 at 04:59
  • /bin/bash is there some general way to change where python is searching for packages so it will register the packages if I'm using the IDLE as well? – whearyou Feb 16 '12 at 05:21
  • you need to edit or create the file .profile in your home directory. (sorry for the unnecessary question about your shell). if it already exists, put the export line at the bottom of it. otherwise, make the export line the only line in the file. then reopen your terminal and the setting should persist when running python from the command-line. it appears that it should work from the IDLE as well, but if you are using an older version, it may not. – dlchet Feb 16 '12 at 05:28
  • so my home directory is the directory the shell opens in when I start the shell? what file name and what extension should i put on the .profile file? thanks a bunch checkorbored – whearyou Feb 16 '12 at 05:34
  • yep - or if you are in a different directory, you can get back to home by doing 'cd ~'. the file needs to be named exactly '.profile' - no name before the dot. – dlchet Feb 16 '12 at 05:37
  • the simplest thing to do is type, in the shell, exactly what i have here: `echo 'export PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH"' | cat >> ~/.profile` – dlchet Feb 16 '12 at 05:40
  • success with the shell! i acessed the .profile file manually. for your text, what does the part after $PYTHONPATH"' do? however, this solution will not work with IDLE. do you have any advice for what i should do, or should i set up a new stackoverflow question? – whearyou Feb 16 '12 at 05:45
  • its definitely time for a new question (and an accept on this one, if in fact it is working?). however, from what i can see, it should work if you are launching IDLE from the terminal. if you are launching it in a different way, you will have to modify the environment as part of that launch. when you make the new question, be sure to include information like your OS and how you are launching IDLE. (you did try re-launching it, right?) – dlchet Feb 16 '12 at 05:52
  • i did relaunch. i launch IDLE by using a shortcut to the version that unpacked when i installed Python 2.7 from the .dmg file; its at /Applications/Python 2.7 . ideally, this would be the IDLE that i would use. how do i launch IDLE from the command line? – whearyou Feb 16 '12 at 06:58
  • If you install Python packages with MacPorts, use the Python that MacPorts installed. Don't try to run them with another Python (like the system Python) by setting `PYTHONPATH`; while it may work in some cases, it won't in other cases and just adds complications. That is defeating the purpose of using a third-party package manager. – Ned Deily Nov 07 '12 at 23:32