7 Replies - 849 Views - Last Post: 15 October 2014 - 02:54 PM Rate Topic: -----

#1 numbers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 14

This Idiot needs help on MacOSX.

Posted 15 October 2014 - 07:55 AM

Alright so everyone goes on and on about how much easier Python is than almost every other language. Unfortunately, I'm a dope. I know a little bit of C++ but I've decided to learn Python. But I can't even get started.
I have a Macbook.
Here's how far I've gotten:
  • I've downloaded Textwrangler and Python.
  • I've saved Python to my desktop.
  • I created a folder on my desktop called "Pythons"
  • I go into Textwrangler and do the typical >>> print ("hello world").
  • I save it as hello.py in the "Pythons" folder on my desktop.
  • I go to terminal and type hello.py and get an error. I'm not looking at it right now, but I believe it says "error... does not exist."


Can someone help me out? What am I doing or what did I do wrong? Is it how I saved everything?
On Xcode I could just run the programs, but Python requires a text so I'm lost as to whether I did everything right.
Everything online seems geared toward Windows users and there's really no book that explains how to ACTUALLY get STARTED with Python on Mac OS.

Is This A Good Question/Topic? 0
  • +

Replies To: This Idiot needs help on MacOSX.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,127
  • Joined: 12-June 08

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 08:00 AM

There seems to be plenty of documentation on python and macs. Heck python COMES on a mac.

Quote

Can someone help me out? What am I doing or what did I do wrong? Is it how I saved everything?

You are vague on the error and what you were doing to execute so here's a smattering of typical fixes.

https://www.python.org/download/mac
http://docs.python-g...ng/install/osx/

Quote

To run your script from the Terminal window you must make sure that /usr/local/bin is in your shell search path.

https://docs.python..../using/mac.html

Quote

PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:

#!/usr/bin/env python

and run

sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py

https://stackoverflo...out-having-to-t
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10731
  • View blog
  • Posts: 18,362
  • Joined: 19-March 11

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 08:13 AM

First thing: you don't need to install python on a mac. Open up the terminal, type "python". You're in a python shell, and you can execute python code. This is where you'll see the >>> prompt, for example

/Users/jpk/:546 $ python 
Python 2.7.8 |Anaconda 2.0.1 (x86_64)| (default, Aug 21 2014, 15:21:46) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> print "hello world"
hello world
>>> 



You might not see exactly that text, because I'm running the Anaconda distribution and you'll be running the default Mac OS distro, but it'll be something like that.

Second, here's the simplest way to create and run a simple python program:
  • create a text file with some python code in it (something as simple as "print "hello, world" will work)
  • save the file somewhere (for example, in your Desktop folder, as "hello.py"
  • navigate to that directory in the terminal and type python hello.py


For example, I've created the hello.py file in my desktop folder. Now I do this:

[[email protected]/~: Wed Oct 15 11:10] 
/Users/jpk:551 $ cd ~/Desktop
[[email protected]/~/Desktop: Wed Oct 15 11:10] 
/Users/jpk/Desktop:552 $ python hello.py
hello, world
[[email protected]/~/Desktop: Wed Oct 15 11:10] 
/Users/jpk/Desktop:553 $ 



There are many books on learning python, several of which are free on line. "Python the Hard Way" is well-regarded, and it should get you up to speed. Google it, and start working through it.
If you're not familiar with working in the terminal, you should work on that. There are many resources out there for working in the command line on unix, including a book from the Free Software Foundation which is free on line.
Was This Post Helpful? 1
  • +
  • -

#4 numbers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 14

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 09:00 AM

Awesome. Thanks kiparsky. That's where I was messing up. I thought that just typing hello.py would run it but now I see that I have to navigate to it. I've tried finding a more detailed explanation in books and other websites but most only tell you "save. go to terminal. run it." But I'd yet to find someone to show me how to actually get to where I needed to be.
As I said, I'm a dope. I'll give this a try and post back on my progress.
Danke.
Was This Post Helpful? 0
  • +
  • -

#5 numbers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 14

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 01:59 PM

-bash: Users/me/Desktop/Pythons/hello.py: permission denied

Now what?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,127
  • Joined: 12-June 08

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 02:00 PM

Run the script as a superuser.. throw in 'sudo' before it.
Was This Post Helpful? 0
  • +
  • -

#7 numbers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 14

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 02:14 PM

Got it going! Many many thanks to you both.
I feel like donkey from shrek... I'm on the road again...
but I'm sure I'll be back soon.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10731
  • View blog
  • Posts: 18,362
  • Joined: 19-March 11

Re: This Idiot needs help on MacOSX.

Posted 15 October 2014 - 02:54 PM

Yeah, let's not use sudo for that. If you want to make a script executable by name, you need to do two, maybe three things

First, you need to put in the hashbang line. This is a line that tells your shell what program to use in executing this program. (ie, what program understands this as data). In this case, the program is the one located at /usr/bin/python, so it looks like this:

#!/usr/bin/python


Then, you need to set the file to be executable. Unix has a useful permissions model, which allows you to set read/write/execute permissions for any file. You should read up on this, since it'll be useful for you to know about it. For now, you're going to just give it read/write/execute permissions for "user" (which is you) and read/execute for "group" and "all". Type this at the prompt:

$ chmod 755 hello.py

(the dollar sign here represents "whatever prompt you're using" - don't actually type the buck character, just the stuff after it)

Now, you have one more thing to consider, and that's the path. Unix looks for programs to execute on a "path" which you can set. By default, the current directory is not on that path, and that's a good thing. So to execute a file that's in the current directory, you have to give it a path reference, not just a filename reference:

$ ./hello.py


The "dot" (.) means "the current directory" and the slash indicates "in that directory", so this means "I want the file called hello.py which is in the current directory". And since it's the only thing on the line, this means "I want to execute it".

If you want to set your path to include the current directory, you're allowed to do that, but I'm going to make you do the research to figure out how that goes. My hope is that by doing that research, you'll learn enough to know at least a little bit about why it's not a great idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1