Hi,

I am a beginner in Python.
I have created 1 script in notepad++ and trying to execute the script from command line but it's giving an error. I have set the PATH of Python in the environment variables also.

Can anyone send me the complete steps for executing the script.

I will appreciate the immediate response.

Thanks

Recommended Answers

All 12 Replies

You should tell us what's the error on the command line (paste the console output), and also paste your program here, it would be easier to help you.

Usually it's as simple as python myscript.py If python isn't in the path you'd need the full path to the python executable.

Have you added to the "Path" something like this??

C:\Python25;

NB: Change python25 to your python folder path!

I'm just beginning to learn Python and programming in general. I'm using a mac. How do I execute a script? My scripts are not in the same folder as Python 2.7. They are saved in a folder on my desktop.

Thanks,
Adam

Thanks for pointing me in that direction. Sorry for the total neophyte questions. I'm still having troubles. Here's what's happeing.

In a Python shell I type
/Users/Home/Desktop/Programs/test2.py
It returns the following:
/Users/Home/Desktop/Programs/test2.py
SyntaxError: invalid syntax
The first slash is highlighted in red.

I also tried doing the same thing in a terminal window.
It returned the following:
File "<stdin>", line 1
/Users/Home/Desktop/Programs/test2.py
^
SyntaxError: invalid syntax

I know I'm probably making a very simple mistake. Any help would be appreciated.

Thanks,
Adam

It's like this you must do it on your terminal window like this:

python /Users/Home/Desktop/Programs/test2.py

or just by the scipt name if you add the shebang:

#!/usr/bin/python

to the first line of your script.

Happy coding!

Cheers

running python code on mac:

path = /Users/yourCompName/Desktop/nameOfProjectFolder

try this in terminal:

cd
cd /path
python filename.py

OK. Getting closer!

In terminal, I typed:
cd /Users/Home/Documents/Programs/LearningPython

Then:
python test1.py

It worked!!!

However it doesn't work if I first type "python" in terminal and then try it again at the >>> prompt.
Also, it doesn't work in a python shell.

I poked around and found that the following works:

In a python shell I type:
execfile("/Users/Home/Documents/Programs/LearningPython/test1.py")

This executes the script called "test1.py"

Is this the only way to execute a script from a shell? In the tutorials I've read they seem to say to just type something like "$ python test1.py". Right now I'm referring to this tutorial.

Any more suggestions/help would be awesome!

OK. Getting closer!

In terminal, I typed:
cd /Users/Home/Documents/Programs/LearningPython

Then:
python test1.py

It worked!!!

However it doesn't work if I first type "python" in terminal and then try it again at the >>> prompt.
Also, it doesn't work in a python shell.

I poked around and found that the following works:

In a python shell I type:
execfile("/Users/Home/Documents/Programs/LearningPython/test1.py")

This executes the script called "test1.py"

Is this the only way to execute a script from a shell? In the tutorials I've read they seem to say to just type something like "$ python test1.py". Right now I'm referring to this tutorial.

Any more suggestions/help would be awesome!

Don't try to use the python shell as a terminal shell, it's a waste of time. The python shell is meant to execute python code. The easiest way to execute a program in the python shell is to use IDLE. In idle, if you open the file test1.py, you only have to reach the F5 key to run your script in the shell.

Maybe you would like to see IPython?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.