I'm currently reading a huge Python book. I've just written my first Python code (from this book).

It's

import sys
print(sys.platform)
print(2 ** 100)
x = 'spam!'
print(x*8)

It works fine and I get the results I want.

The only problem I have right now is running it off the system Shell prompt.

I'm typing

python script1.py

And that doesn't work. I'm also typing

% python script1.py

I'm getting this syntax error

File "<stdin>", line 1
 python script1.py

SyntaxError: invalid syntax

What am I doing wrong? I have tried both Python Command Line and CMD just to test anything.

Edit: It seems that I haven't to place a full directory path. Can someone help me out with this?

Hmm. First check: Is script1.py a pure text file? If not, there's the problem.

Otherwise: Windows or unix-like system?

  • If unix-like try chanting python [I]full/path/to/script[/I] where that full path might be ./script1.py
  • If Windows, try the same sort of thing with the slashes tilted the other way

If you still have trouble: Can you invoke python to create an interactive Python session with the >>> prompt? If that works, then you may have a newline incompatibility (though it should not matter). Try looking at your text file with something like od -c (octal dump utility).

If you did not get a prompt, then expect to find that your PATH environment is set so you have something spelled 'python' on your path ahead of the real one. If in unix-like environment, chant type python and if the result is confusing try which python I don't know how to do that in Windows...

Edit: It seems that I haven't to place a full directory path. Can someone help me out with this?

The directory either has to be in the PATH or you can call it in the current directory with
python ./script1.py
or just
./script1.py if on Linux and the script is executable with the bash shebang.
http://code.activestate.com/lists/python-tutor/71566/

Lets try something else. Please chant this at the command line:

rm -f check.py
echo "print('that worked')" > check.py

python ./check.py

What I expect is that you will see something that looks approximately like this on your terminal:

% rm -f check.py
% echo "print('that worked')" > check.py
%
% python ./check.py
that worked
%

Where the line without the % prompt is the output from the script.

If that does not work, then please look back at my previous post and try those things in order. If it does work, then figure out how your script is different from mine: Is it in the same directory? Is it really just a text file? Check it by chanting od -c script1.py and compare that to the output from od -c check.py . Maybe that will be sufficient to solve your issue. If not, ask again...

Hmm. First check: Is script1.py a pure text file? If not, there's the problem.

Otherwise: Windows or unix-like system?

  • If unix-like try chanting python [I]full/path/to/script[/I] where that full path might be ./script1.py
  • If Windows, try the same sort of thing with the slashes tilted the other way

If you still have trouble: Can you invoke python to create an interactive Python session with the >>> prompt? If that works, then you may have a newline incompatibility (though it should not matter). Try looking at your text file with something like od -c (octal dump utility).

If you did not get a prompt, then expect to find that your PATH environment is set so you have something spelled 'python' on your path ahead of the real one. If in unix-like environment, chant type python and if the result is confusing try which python I don't know how to do that in Windows...

Since your code in the other reply didn't work, I'll reply to this post.

I used the code you gave me

rm -f check.py
echo "print('that worked')" > check.py

python ./check.py

I get an invalid syntax at the check part here

rm -f check.py

It returns the message "Invalid Syntax."

I'd like to ask you some question regarding your post.

I hope you don't mind the dumb questions.

My first question is, what do you mean by pure text file?

Here's some info that might also help (or I hope it does).

I'm running Python 3.0.1 on Windows. I have looked online for the solution to my problem. The website that I went to told me to look for the directory of my Python 3. I did, and copied and pasted it after going to System, Edit Variables (something like that), and adding the directory to the Path file. It seems that Python will not register my directory.

I put Python on my Desktop. What I mean by that, is that when I was installing Python, I unpacked all of the Python files into my Desktop. Would that cause any problems?

I only did it because I like to reach the Python directory with ease.

Edit: I tried to post the code in the command line, but it wont recognize rm. I also did it in the Python command line, and I have no luck either. Should I do a fresh install and do it in the correct Directory?

OK. First thing: You are using windows, and I've been assuming Linux. From the Windows command prompt, rm, cat, od and the other unix utilities do not work, so of course you get a syntax error. I assumed Linux because in a previous post you mentioned tring python ./script1.py after a post telling you to use .\script1.py if you were on Windows.

By pure text file, I mean something that has only ascii text in it, created by a text editor, not a WYSIWYG editor. I think Windows has a horrible little text editor called textpad. Do not use Word or wordpad or anything that knows how to do multiple fonts, bold or italic text etc. Just "pure" text (no way to do formatting). Text editors are sometimes also known as programmers editors. You can use any that you have.

Python location: If you can, from the command prompt, invoke either python or python3, and get the >>> prompt then having it on the desktop is no problem. If not, then you should install it in the 'normal' location (I don't really do windows, so I'm not sure where that would be). If you don't know where that is, then I suggest you re-download a self-installing python package. If you have installed python 3, do you have a python.exe or only a python3.exe? (in python's bin directory). If only python3, then of course you must say python3 when you invoke python.

Script location: From your command prompt window chant dir Do you see your script1.py? If not, then you must cd to the directory where your script1.py is located. (or move the script to where your command prompt has its current working directory).

Quick note - Notepad is the simple editor that comes with Windows. Textpad is a much more feature rich editor sold by Helios Software (www.textpad.com) and should not be maligned by mistaking it for Notepad.

I'm just sayin'

my error. apologies to anyone i offended

No offense taken. I just wanted to correct a minor point.

Sorry for not replying sooner, was busy with school.

Anyways, I found out that the only way to fix my problem was to reinstall Python to the correct Directory. From now on, any programming language I try to learn will be installed correctly haha.

I also had to do the cd C:\Python30 command on the command prompt.

Just found out something rather cool.

A very easy way to run a Python file on Windows through the command prompt.

Just open up the command prompt, and just type the name of your file. Mine was script1.

No need to add .py. Windeos uses Registry to look for files in the CMD. And when it finds your program (Python) it knows that it's a Python program.

Pretty neat.

hey could you please help i'm facing the same problem..

Ankur_5, I looked up the members in this thread and most have been away for 4 or more years. One of the things you learn is to not dredge up old discussions like this. Just make your new post and if you feel it's relevant, add a link to the old discussion.

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.