0

I am new to Python and mac OS. I've Installed py27-pygtk-2.24.0_3 from macports.
I tried to run the following:

#!/opt/local/bin/python2.7
import gtk
window=gtk.Window()
window.connect("destroy", gtk.main_quit)
button=gtk= gtk.Button("Hello")
window.add(button)

window.show_all()\gtk.main()

I am executing the file through Python Launcher and get this error:

Import error:No module named GTK

I spent the whole day searching for answer. Can someone help me and make it work?

Yoel
  • 9,144
  • 7
  • 42
  • 57
  • How have you installed `py27-pygtk`? Where was it installed into? By any chance do you have several `Python` versions installed on your system? [This question](http://stackoverflow.com/q/7317921/3903832) might prove useful. – Yoel Sep 26 '14 at 11:44
  • I typed in Terminal: $ sudo port install py27-pygtk, I am not sure if another Python was installed. Existing Python is ver 2.7.5 – Pawel Iwaniuk Sep 26 '14 at 11:50
  • What is the output of `port select --list python` and `port select --show python`? – Yoel Sep 26 '14 at 11:57
  • So, should I install another Python to make it work? Using those two lines used in the link. Can I use existing Python instead? – Pawel Iwaniuk Sep 26 '14 at 12:04
  • Those lines don't install anything. They just show which versions you have currently installed, and which one is the default. – Yoel Sep 26 '14 at 12:05
  • Available versions for python: none (active) python25-apple python26-apple python27 python27-apple pawels-mbp:~ paweliwaniuk$ port select --show python The currently selected version for 'python' is 'none'. – Pawel Iwaniuk Sep 26 '14 at 12:06
  • I just input: pawels-mbp:~ paweliwaniuk$ sudo port select --set python python27 Password: Selecting 'python27' for 'python' succeeded. 'python27' is now active. pawels-mbp:~ paweliwaniuk$ port select --show python The currently selected version for 'python' is 'python27'. – Pawel Iwaniuk Sep 26 '14 at 12:14
  • Tried to run original script and still the same error message – Pawel Iwaniuk Sep 26 '14 at 12:15
  • You have to reinstall `pygtk` onto that version. Please see my answer for details. – Yoel Sep 26 '14 at 12:37

1 Answers1

0

First of all, you should clean up all old installations of pygtk which might be corrupt:

sudo port clean --all -f py27-pygtk

Now, since you have several versions of Python installed on your system, as shown by the command:

port select --list python

you have to select the correct one into which macports will install pygtk, as follows:

sudo port select --set python python27

Only then you can install pygtk as follows:

sudo port install py27-pygtk
Yoel
  • 9,144
  • 7
  • 42
  • 57
  • sudo port install py27-pygtk Warning: xcodebuild exists but failed to execute Warning: Xcode does not appear to be installed; most ports will likely fail to build. ---> Computing dependencies for py27-pygtk ---> Cleaning py27-pygtk ---> Scanning binaries for linking errors ---> No broken files found. – Pawel Iwaniuk Sep 26 '14 at 14:04
  • Follow [these steps](https://guide.macports.org/chunked/installing.html#installing.xcode) in order to install `xcode`. – Yoel Sep 26 '14 at 14:19
  • Is there anything else to be set in Mac? I bought it 2 days ago, and I am not familiar yet with OS at all. – Pawel Iwaniuk Sep 26 '14 at 14:22
  • I don't own a Mac so I have no idea. Try googling something like _configure new mac_ and see what's relevant for you. Whenever I get a new OS, I learn it for the first few months and reinstall from fresh once I'm confident enough not to repeat the mistakes I've done the first time around. Are you still getting an error when attempting to import `gtk`? – Yoel Sep 26 '14 at 14:26
  • I am going mad slowly. I can not install Xcode because my OS is 10.9.3 and needs to be 10.9.4 up. Do I need to update OS? – Pawel Iwaniuk Sep 26 '14 at 14:34
  • If it doesn't let you install on 10.9.3, I guess you should. I think it's a good idea anyway, and shouldn't take too long. – Yoel Sep 26 '14 at 14:41
  • Have you upgraded your OS? Was the installation of `pygtk` successful this time around? – Yoel Oct 01 '14 at 16:21
  • I gave up. OS was upgraded, Xcode installed, I went through dozen tutorials still nothing. At the moment I installed Eclipse with PyDev, I am able to run create Python programs... but can not create PyDev project. When New project is created I can not add PyDev module using PyDev perspective. PyDev icon is there but when press OK window is gone. So I can not type anything in the project. As well I can not open this project again and create new with the same name. Is it some kind of sign to leave Python alone? – Pawel Iwaniuk Oct 04 '14 at 20:12
  • Oh, sorry to hear. What was the problematic output regarding `pygtk` installation? I expected it to work properly the second time around... – Yoel Oct 04 '14 at 20:16
  • Import error:No module named GTK. Do you have any experience with Eclipse and PyDev? – Pawel Iwaniuk Oct 04 '14 at 20:34
  • No, sorry... But the `import error` surprises me. Were there any errors on the reinstallation of `pygtk` itself with `sudo port select --set python python27` and `sudo port install py27-pygtk`? – Yoel Oct 04 '14 at 20:44
  • That's why I gave up, installation without any errors, but result the same. I spent hours trying. But with Eclipse I hope more luck – Pawel Iwaniuk Oct 04 '14 at 21:04
  • I see, maybe try cleaning up the old installation before, with the command `sudo port clean --all -f py27-pygtk` and then reinstall it again. – Yoel Oct 04 '14 at 22:28
  • Yes now I can import GTK. It was long way. Thank you – Pawel Iwaniuk Oct 06 '14 at 20:22
  • Great, I've updated my answer to reflect our new findings for future reference. Please consider [accepting](http://meta.stackexchange.com/q/5234) it by clicking the check-mark that is to the left of the answer. Note that there is absolutely no obligation to do this. – Yoel Oct 06 '14 at 21:13