Sign in to follow this  
Nameless_

How Do You Save And Run A Script Of Python?

Recommended Posts

Hi guys... I'm trying to learn Python. Well. IT's kinda working. I understand the rules and stuff, but lets say I want to save a script of something... Lets say I have a script/line like this:

 

print 'This is a test...'

And I did that in the IDLE (Python Shell thingymabob)

 

QUESTION ONE:

How do I save it, and then run it?

 

I tried just saving the shell as test1.py, and then use the Python Launcher by dragging the test1.py from one window to another, but this stupid window always pop up, saying:

 

File type binding

PythonLauncher is not the default application for all Python script types. You should fix this with the Finder's "Get Info" command.

 

See "Changing the application that opens a file" in Mac Help for details.

 

And three different windows all named "Terminal - bash 80x24" also pops up, but all three have different content in them, and I have no idea what it does. Not to mention another grey window that also pops up... :/ Which runs the program if I press the run button, but I don't get the settings of it either.

 

So confusing. :/

 

Also... The python shell seems to not need programs to run down from one to another. And when I press "restart shell" on the top taskbar thing in mac, it just does nothing, but add a line at the bottom of the shell with a "RESTART" word on it. So...

 

QUESTION TWO:

So... what does the restart thing does, if it doesn't clear the python shell anyway?

 

Now, lets say I am trying to write a script like this:

n = raw_input("Please enter your name: ")print nn = input("Enter a numerical expression: ")print n

This doesn't work, because when I press enter after the first line... it just prints "Enter a numerical expression: ", so I can't write the second line (print n)

QUESTION THREE

So... how do I write this script? And how do I run it, after I finally learn how to write this script?

 

That's all for now. I've got plenty of questions coming (and hopefully it will add some life to the forums), but yeah. The main thing is running the script and stuff. I hope you can all understand my questions. :/

Jeez. Starting to actually program is hard.

Share this post


Link to post
Share on other sites

If i'm not wrong IDLE let's you can write the whole script and then execute it. You can save the script and then it on prompt. One bad practice i have seen in the code that you are using same variable name for both inputs. Why is that ?Let me correct your code one by one.n = raw_input("Please enter your name: "); - Add the semicolon after this line. print n; - Again add the semicolon after this line so that you can add more lines to your code. Why ? semicolon pushes the execution to next command till there is no statement left. Hope that works out. By the way i'm using IRONpython on my computer so some changes may apply because Ironpython is from microsoft codeplex.

Share this post


Link to post
Share on other sites

If i'm not wrong IDLE let's you can write the whole script and then execute it. You can save the script and then it on prompt. One bad practice i have seen in the code that you are using same variable name for both inputs. Why is that ?
Let me correct your code one by one.

n = raw_input("Please enter your name: "); - Add the semicolon after this line.
print n; - Again add the semicolon after this line so that you can add more lines to your code. Why ? semicolon pushes the execution to next command till there is no statement left.

Hope that works out. By the way i'm using IRONpython on my computer so some changes may apply because Ironpython is from microsoft codeplex.


Hmm... It didn't seem to work. :( But then, I use a mac... so yeah. In the end, I had to do it in the mac version of notepad, which is textedit, save it as test.py and then run it. :(

But I still don't get what terminal does... and what the grey box that pops out is. :( The reason why I'm using the same variable name for both inputs is because that is exactly what it says in the tutorial that I'm using to learn python. :P I'm doing their excercises one by one.

I kinda figured out that "Terminal" kinda runs the program? I don't know how... but I'm guessing it does. If thats the case, then what is the point of Python Launcher?

Share this post


Link to post
Share on other sites

Hmm... It didn't seem to work. :( But then, I use a mac... so yeah. In the end, I had to do it in the mac version of notepad, which is textedit, save it as test.py and then run it. :(
But I still don't get what terminal does... and what the grey box that pops out is. :( The reason why I'm using the same variable name for both inputs is because that is exactly what it says in the tutorial that I'm using to learn python. :P I'm doing their excercises one by one.

I kinda figured out that "Terminal" kinda runs the program? I don't know how... but I'm guessing it does. If thats the case, then what is the point of Python Launcher?


Have you checked the new code ? try adding semicolon to each statement and write the program in one line in prompt. don't press enter till you write last statement. it seems to work with python 2.4 and ironpython 2.6. I don't know what you are trying with mac but it works irrespective of operating system.

n=raw_input("your statement");print n; n=raw_input("another numberical exp");print n;

above code written in one line should work should work.

Share this post


Link to post
Share on other sites

Well, I haven't run a python script on any other platform than Linux, but I do know how to run it in Windows, too. Not sure about mac, but since it's Unix you might want to try my method.
Open up command line and run the following:

python path/to/your/script/here.py
Basically, I have after a small research that you launch the python interpreter just as you do in Linux, that is, by typing "python" in terminal and executing. In the above code you execute the file with the program "python". That's it.

I don't know about your restart problems but it might be that it restarts the shell as it says. Not necessarily python. Maybe it deletes the session the variables and functions. You might want to try if the variables are still working after restart.

BTW, I used your code in my terminal and it ran normally.
Edited by Baniboy

Share this post


Link to post
Share on other sites

Have you checked the new code ? try adding semicolon to each statement and write the program in one line in prompt. don't press enter till you write last statement. it seems to work with python 2.4 and ironpython 2.6. I don't know what you are trying with mac but it works irrespective of operating system.

n=raw_input("your statement");print n; n=raw_input("another numberical exp");print n;

above code written in one line should work should work.

Hey, that works! Thanks. :) Sorry... it doesn't seem like much at the moment, but I'm still learning. :) So... you don't know what the grey box nor what "Terminal" is? What about the "Python Launcher" and "Build Applet"?
I hope its the same in Macs as it is in Windows....

Share this post


Link to post
Share on other sites

I just wanted to add that using the same variable name helps in making more efficient use of allocated memory. It is not always possible to do so, and it makes the code harder to read if it is not accompanied with comments that explain the use of the variable. BTW, I have seen seasoned developers with well over a decade of experience naming their variables as query1, query2, query3 etc and that does little to help anyone else understand what those variables are supposed to relate to. Most IDEs today have a refactoring feature to rename variables wherever they appear within a given scope, so there's no reason to leave the variable names at their defaults.BTW, in the OP post, saving the shell is not the right term - it saves the script within the .py file. The Python script could just as easily have been written using Notepad or Eclipse. IDLE serves primarily as a learning tool to get people started on learning the Python syntax. Sure, people do continue to use it but the purpose is better served by other editors.I'm not quite sure of what the OP meant by restarting the shell - the script is where the code is stored and memory for the execution is allocated when the script is run. As soon as the script execution is complete, the memory is deallocated. The interactive shell is simply a means to try some code. It's a worksheet of sorts. When running in a command line, it is not uncommon for programs to clear the entire screen because sometimes it is useful to be able to see the output of the previously-run commands.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this