Benjamin Danek
Benjamin Danek

Reputation: 13

Python Starter script

To start, am incredibly new to python so please bear with me.

I want to write a very simple script to demonstrate some operations with the list data type. This is an abridged version of my script (It's named listTest.py)

#!/usr/bin/python

import sys
print(sys.version_info) 
print “successful start”
print “ “

listAl = [ a, b, c, d, e, f, g]
listNu = [ 1, 2, 3, 4, 5, 6]
print listAl
print listNu

To run it, I open op my terminal with spotlight search on my mac (macOS 10.12.6) and type in

python

which runs python 2.7.10. I also have python 3.5.1 which I know I can run with

python3

With my python 2.7.10 prompt open I type the following (left the python starting entry for complete transparency)

Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> listTest.py

and get the following message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'listTest' is not defined 
>>> 

Now, I have no clue what to do at this point. At first I was concerned that I hadn't installed python properly (which I hadn't since python 2.7.10 came with this OS by default) so I checked that I had all my essential components to python. I was missing pip, so I installed pip (here is the pip and python directories)

/usr/local/bin/pip
/usr/bin/python

Then I was concerned I had my listTest.py text file (UTF-8 encoding) in the wrong directory, so I checked it's location on my computer, by typing

ls Desktop

yep, it's there,(which I think is alright?) along with some other text files that crashed and burned in the same way. I also tried installing a virtual environment (pyvenv) to run the script in hoping it would mitigate the issue but I got the same exact error.

What can I try now? Thank you in advance for being patient.

Upvotes: 0

Views: 523

Answers (2)

omkaartg
omkaartg

Reputation: 2777

Copy the whole program and paste it in shell(begginer method),It will/should look like this:

Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>#!/usr/bin/python
>import sys
>print(sys.version_info) 
>print “successful start”
>print “ “
>listAl = [ a, b, c, d, e, f, g]
>listNu = [ 1, 2, 3, 4, 5, 6]
>print listAl
>print listNu

IT WILL SPLIT IN LINES AND REFORMAT ITSELF.
This will execute program line by line...

Upvotes: 0

Ali M
Ali M

Reputation: 821

Python has two basic modes: script and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory.

You can run python in interactive mode with entering command python or python3. When you have a file that you want to run you should pass the file address as an argument to command like:

python listTest.py

or

python3 listTest.py

If you are getting "can't open file 'listTest.py': [Errno 2] No such file or directory" error It's because you are in the wrong place! open the folder in your File Explorer then from the address bar copy the address and try to change directory to the directory that your files exists in:

cd Directory_That_ListPy_Is_In
python ListTest.Py

Here is some tutorial that helps you master the terminal navigating files and folders.

Upvotes: 2

Related Questions