6 Replies - 816 Views - Last Post: 15 May 2013 - 07:18 AM Rate Topic: -----

#1 chemcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-May 13

Running a Basic Script in OS X Mountain Lion

Posted 14 May 2013 - 02:16 PM

I've been learning to code python for the past two weeks using code academy and now that I feel ready to start making programs, I realized I don't know how to run them. I have both python 3.3.1 and 2.7.2 installed in OS X mountain lion 10.8.2. So far I have tried typing in the file location in the terminal, dragging the .py file into the terminal, typing python before the file name and many other random things. I have tried looking up tutorials: the google guy http://www.youtube.c...h?v=tKTZoB2Vjuk put everything in a main function and then use a "boilerplate" command at the end (whatever that is) but I couldn't get that to work, I've tried putting the #!/usr/bin/python -tt at the beginning but it won't work. I wonder if it has something to do with my textedit. Ultimately I change the file extension back to .py but every time I open the file, it converts it back to rich text. Most all the errors I get are syntax errors.

My code as of now looks like this

print 'Hello World!'

def keq(HA, A):
	pKeq = HA - A
	Keq = 10^ (-1 *pKeq)
	return Keq

def hendhas():
	Print "What parameters do you have? \n 1 - pH \n 2 - pKa \n 3 - A- \n 4 - HA"

	hendhas()




Is This A Good Question/Topic? 0
  • +

Replies To: Running a Basic Script in OS X Mountain Lion

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • Joined: 19-March 11

Re: Running a Basic Script in OS X Mountain Lion

Posted 14 May 2013 - 03:20 PM

You have two issues here, at least. First is your code. You've defined two functions here, one of which does some arithmetic on its parameters, and the other of which never returns (because it calls itself recursively without end).

You don't call either of those functions, so all I'd expect to see here is the text "Hello World" when you run it.


Second issue is format.

Quote

Ultimately I change the file extension back to .py but every time I open the file, it converts it back to rich text.



You can probably convince that editor to save the file as raw text, but it's probably easier to use a real text editor. If you go into the terminal, there's vi and emacs which are the two big ones. Both of them are good, both of them have learning curves. There's also pico, which is pretty easy and may well be installed by default (I don't know if it is - at the shell prompt, that is, in the Terminal window, type "which pico", if you get any output, like "/usr/bin/pico" then it's installed).

Pico is not an editor I'd recommend, but it's dead simple to figure out and it won't give you file format issues.

There are also a number of code-oriented editors. Notepad++ is popular and free, and again it won't cause you issues with format.



EDIT: third error is the capital P in Print. Python is case-sensitive

This post has been edited by jon.kiparsky: 14 May 2013 - 03:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Running a Basic Script in OS X Mountain Lion

Posted 14 May 2013 - 03:40 PM

Notepad++ won't work on OS X as it's a Windows-based editor.

You could try TextWrangler which is free. I'm partial to Sublime Text 2, but it's not free.

Run your script from within Terminal with python filename.py. If this doesn't work, it's possible python is not in your path; you could check this with which python in Terminal. For example:

$ which python
/usr/bin/python
$

If nothing is printed after issuing that command, then python is not on your path.
Was This Post Helpful? 1
  • +
  • -

#4 chemcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-May 13

Re: Running a Basic Script in OS X Mountain Lion

Posted 14 May 2013 - 04:10 PM

So I just created a basic program that's supposed to pint hello world.

The code says
 
print 'Hello World"



Then I saved it as what.py in TextWrangler and saved it to my desktop.

Then I went to the terminal and here is what happened:

Last login: Tue May 14 16:54:12 on ttys000
CB-MacBook-Pro:~ CB$ which python
/usr/bin/python
CB-MacBook-Pro:~ CB$ python what.py
python: can't open file 'what.py': [Errno 2] No such file or directory
CB-MacBook-Pro:~ claybrow$ 


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • Joined: 19-March 11

Re: Running a Basic Script in OS X Mountain Lion

Posted 14 May 2013 - 04:27 PM

you need to either be in the directory with the source file or else give the absolute path to it.
If it's in a directory called "python" under your home directory, then you would either do:

CB-MacBook-Pro:~ CB$ cd python
CB-MacBook-Pro:~ CB$ python what.py



or

CB-MacBook-Pro:~ CB$ python python/what.py




Quote

Notepad++ won't work on OS X as it's a Windows-based editor.


I guess you're right. My bad. (I always use vi or emacs...)
Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Running a Basic Script in OS X Mountain Lion

Posted 15 May 2013 - 04:03 AM

View Postchemcoder, on 14 May 2013 - 07:10 PM, said:

So I just created a basic program that's supposed to pint hello world.

The code says
 
print 'Hello World"



Then I saved it as what.py in TextWrangler and saved it to my desktop.

Then I went to the terminal and here is what happened:

Last login: Tue May 14 16:54:12 on ttys000
CB-MacBook-Pro:~ CB$ which python
/usr/bin/python
CB-MacBook-Pro:~ CB$ python what.py
python: can't open file 'what.py': [Errno 2] No such file or directory
CB-MacBook-Pro:~ claybrow$ 



Like jon.kiparsky said (except if the folder is under where you are in the hierarchy, you only need the relative path, which is what jon showed). Details are:

Terminal opens in your home directory by default. You said you saved it to the Desktop, which means it's in the Desktop folder under your home directory, so you either need to change into the Desktop directory or provide the relative path to the file from the home directory.

You should spend some time getting to know how your computer and OS work before programming. You don't want to start messing with file I/O and end up blowing away huge chunks of your HD because you don't understand how it all works together.
Was This Post Helpful? 1
  • +
  • -

#7 chemcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-May 13

Re: Running a Basic Script in OS X Mountain Lion

Posted 15 May 2013 - 07:18 AM

View PostJackOfAllTrades, on 15 May 2013 - 04:03 AM, said:

View Postchemcoder, on 14 May 2013 - 07:10 PM, said:

So I just created a basic program that's supposed to pint hello world.

The code says
 
print 'Hello World"



Then I saved it as what.py in TextWrangler and saved it to my desktop.

Then I went to the terminal and here is what happened:

Last login: Tue May 14 16:54:12 on ttys000
CB-MacBook-Pro:~ CB$ which python
/usr/bin/python
CB-MacBook-Pro:~ CB$ python what.py
python: can't open file 'what.py': [Errno 2] No such file or directory
CB-MacBook-Pro:~ claybrow$ 



Like jon.kiparsky said (except if the folder is under where you are in the hierarchy, you only need the relative path, which is what jon showed). Details are:

Terminal opens in your home directory by default. You said you saved it to the Desktop, which means it's in the Desktop folder under your home directory, so you either need to change into the Desktop directory or provide the relative path to the file from the home directory.

You should spend some time getting to know how your computer and OS work before programming. You don't want to start messing with file I/O and end up blowing away huge chunks of your HD because you don't understand how it all works together.


Yeah, you're probably right. I should spend a little more time learning about my OS. I haven't done any programming since I switched to Mac and before I was doing languages that had built-in compilers in the scripting software. anyways I finally got the script working. Thanks for everyone's help on this super basic question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1