syntax error in first-time user script

Peter Otten __peter__ at web.de
Fri Mar 24 05:36:07 EDT 2017


john polo wrote:

> Greetings,
> I am attempting to learn Python. I have no programming background. I'm
> on a computer with Windows 7. I checked the PATH in System Variables and
> Python and Python\Scripts are in the path. I have a book, Python for
> Biologists and it supplies example scripts, one is called comment.py.
> The output from comment.py is
> 
> Comments are very useful!
> 
> I use cd to get to the directory with the example scripts and open a
> command prompt with "python" and get the prompt. I type
> 
> "python comment.py"
> 
> and get
> 
> "    File "<stdin>", line 1
>          python comment.py
>                                      ^
> SyntaxError: invalid syntax

First, welcome, and a big Thank You for providing the tracebacks!

After the

C:> cd

(the C:> is a placeholder for whatever prompt you see) do not invoke python 
without arguments, invoke it with the script name:

C:> python comment.py

> "
> 
> Sorry, I realize this may not all line up like it does when I'm typing
> this in the email. The caret/error pointer is under the t in comment. I
> am not sure what is causing this error.
> 
> If I type
> 
> "import comment.py"

In this case you are already running the Python interpreter (remember you 
invokded it with 

C:> python

) and that will successfully import the comment module (you must not give 
the .py extension, that's automatically added when the interpreter searches 
for the module) and then try to import the py submodule which fails (if 
there were such a module it would be in the file comment/py.py where comment 
is a directory and py both the filename and the extension).

> 
> the output is
> 
> "Comments are very useful!
> Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
> ModuleNotFoundError: No module named 'comment.py'; 'comment'
> "
> 
> If I use IDLE shell:
> 
> "python comment.py"
> 
> "SyntaxError: invalid syntax"
> 
> I know I can open the file in the editor and use F5, but shouldn't I be
> able to use a command from shell? How do I run comment.py from a command
> line without error? Or is that not possible?
> 
> cheers,
> John





More information about the Python-list mailing list