[Nottingham] Python scripts and Fwd: Thursday 16 May

Martin martin at ml1.co.uk
Wed May 15 09:24:47 UTC 2019


Nigel,

Surprised none has come back to you on this one. Sorry for the slow
response!


We're over in Ye Olde Trip this Thursday for fun bits and pieces. Hope
you can make it!


For the Python bits, there's a few bits of syntax to follow...

The ">>>" indicates that you are in a Python interpreter. To run script
files from that prompt, you need to do a few Python gymnastics! See:

How to execute a file within the python interpreter?
https://stackoverflow.com/questions/1027714/how-to-execute-a-file-within-the-python-interpreter

Another way is to format your script as a module and then "import" it
and execute the module...


For an example of running a python script from a *bash prompt* on my
machine for various versions of python, I get:

#####

martin :) $ chmod +x hello_world.py
martin :) $ chmod +x hello_world.py.no_hash_pling
martin :) $
martin :) $ ./hello_world.py
Hello World
martin :) $ ./hello_world.py.no_hash_pling
./hello_world.py.no_hash_pling: line 5: syntax error near unexpected
token `'Hello World''
./hello_world.py.no_hash_pling: line 5: `print( 'Hello World' )'
martin :( $
martin :( $ python ./hello_world.py
Hello World
martin :) $ python2 ./hello_world.py
Hello World
martin :) $ python3 ./hello_world.py
Hello World
martin :) $ python3.6 ./hello_world.py
Hello World
martin :) $ python hello_world.py.no_hash_pling
Hello World
martin :) $ python --version
Python 3.5.5
martin :) $ python2 --version
Python 2.7.15
martin :) $ python3 --version
Python 3.5.5
martin :) $ python3.6 --version
Python 3.6.5
martin :) $
martin :) $ python2
Python 2.7.15 (default, Sep 13 2018, 09:29:39)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ./hello_world.py
  File "<stdin>", line 1
    ./hello_world.py
    ^
SyntaxError: invalid syntax
>>> hello_world.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello_world' is not defined
>>>
martin :) $ python
Python 3.5.5 (default, Apr 18 2018, 13:08:30)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hello_world.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello_world' is not defined
>>>
>>> exec(open("hello_world.py").read())
Hello World
>>>

#####


The two files are attached for reference.


Hope that explains?

Note also that in unix/linux, the filesystem and program names are case
sensitive! (As compared to the case insensitive world of Windows...).


Good luck!

Cheers,
Martin



On 12/05/2019 18:54, NIGEL Black via Nottingham wrote:
> nottingham at mailman.lug.org.uk
> 
>> ---------- Original Message ----------
>> From: NIGEL Black <rnjblack at virginmedia.com>
>> To: mailman-owner at mailman.lug.org.uk
>> Date: 10 May 2019 at 20:01
>> Subject: Thursday 16 May
>>
>> Fellow Penguins,
>>
>> 1. I might not be at the next NLUG meeting so have attached a
>> reference on how to dual boot Linux mint and Windows 10.  It might be
>> of particular interest to Boris. 
>>
>> https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/ 
>> <https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/>
>>
>> 2.  I've recently started on Python programming using version 3.6.7.
>> Calling up python3.6 on the terminal followed by
>>
>> >>>print( 'Hello Nigel' )  works.  However the editor version saved as
>> "hello" in a folder called Python36 doesn't.
>>
>> >>> Python36 hello
>> File "<stdin>", line 1
>> Python36 hello
>> ^
>>
>> SyntaxError: invalid syntax
>>
>> Do I have to set up a path to Python36?   My reference book doesn't
>> mention that.  What exactly do I have to type to get this to work? 
>> Any polite advice welcome.
>>
>> See you soon.
>>
>> Nigel
>>
> 
>  
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: hello_world.py
Type: text/x-python
Size: 89 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/nottingham/attachments/20190515/58aa2026/attachment.py>
-------------- next part --------------
# /usr/bin/env python
#
# 'which python' gives: /usr/bin/python

print( 'Hello World' )



More information about the Nottingham mailing list