Category Archives: download

STARTING WITH PYTHON3 – The very beginning – V2

Journal: uffmm.org,
ISSN 2567-6458, July 5, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is an update of the post ‘STARTING WITH PYTHON3 – The very beginning‘ from July-1, 2019. The original version was not ‘wrong’, but it seems, after a few days later, one can and should improve it. The main motivation for this new version is my experience as a linux programmer working with windows 10: learning by doing I detected a litle more radical approach to begin python programming with win10. This post is part of the overall  topic ‘Co-Learning with python 3‘.

OVERVIEW

In this updated version I will introduce some bits of the win10 environment which you will need for your python programming. Then I demonstrate the very first steps with some lines of code. Very important for all your own activities: in the beginning get you a helpful book to guide your start. After trying many sources I have actually the following recommendations:

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/

THE WIN10-ENVIRONMENT

When you start programming  you need a direct knowledge about your computing environment, in that case the win10 operating system. While linux is built up with an open mind and tries to make everything transparent, win10 is somehow the opposite: it is a closed box, and to work with it in direct contact with the operating system is not very well supported. Thus to work as a programmer with win10 is not what you ideally want. But now we are there and have to get the programming done.

After my first expriments with win10 I detected the following tools as very helpful to start programming: (i) The editor for the system and environment variables and (ii) the powershell. These tools allow you to define the operating system environment for your python programming  in a minimal way.

The editing of the system and environment variables you can reach by the following key-operations:

Editing System and Environment Variables

  1. Hit the windows key together with the key for ‘R’
  2. This opens a small window with a textfield-entry.
  3. In this field you enter the string ‘control’
  4. This opens a page with different topics for to manipulate some aspects of your system (because my computer is in the German-Mode I have the heading ‘Einstellungen des Computers ändern’, something like ‘Change options of the computer’).
  5. Here you select the topic ‘System and Security’
  6. Then another page opens with several topics, one is ‘System’, which you should select.
  7. On this new page you can see at the left border some more topics. The last one mentions something like ‘Extended System Options’. Select this
  8. Before you see the next page you will be asked to enter the Super-User Password. Thus you must be super-user to do this.  To be a real programmer you must have this rights to get a full understanding of what is going on.
  9. Then  the editor for system and environment variables pops up:
Editor for system and enviroment variables

 

  1. The interesting button is in the right down corner, in German: Umgebungsvariablen (‘environment variables’)
  2. Click this button and you will finally see the editor you are looking for:
Finally the editor to edit private and environment variables
  1. For the upcoming actions we need only the path-variable. One can highlight the Path-entry and select it with ‘Editing’ (German: ‘Bearbeiten’):
The window showing all the entries of the path-variable.
  1. Now you can see all the entries of the path variable nicely ordered one after the  other. This is a special service because the path-variable as such (as you will see later when using the power-shell) is one string. Here this string is splitted up into the different sub-strings. This allow you to add some new string or — also very important — to delete a substring. As You can see in the figure there is as last line an entry including the name Python . This line is the whole directory path on the drive C:\ where my actual python version is stored.

CLEAN UP BEFORE

Now, if You know where and how you can edit the basic system variables it can be a good idea to clean up the whole system with regard to older python installations. In my case I had tried  before — as a complete newbie — several python distributions like WinPython and Anaconda in different versions at different locations in the system, this accompanied with different installations of the python language. You can imagine that this caused finally some confusion what is where and what is causing which  effect.

Thus I decided to start from scratch, removing all the old stuff to get a ‘point zero’ for beginning.

The first step was to use the win10 explorer program with the find option in the small text-field up right.

Part of the win10-explorer window showing a find-operation on the whole PC

After you have entered your search-string, in this case ‘python’, you have to say (left up corner) the region, in which you want the search to happen. I have selected the whole PC.

After some while you see all locations as a complete path where the search-string python could be found. When I started my clean-up action there where lots of locations with different python installations, now there is only one left, which I am actually using.

But caution: If you want to delete the old installations you have (i) to look to the uninstall options/ programs to activate these to remove all stuff; (ii) finally you have to enter the editor for system and environment variables (see above) to delete all entries there, which are now obsolet.

GET YOUR PYTHON

Now, if you have set everything with Python to zero get your python version from the python home page: https://www.python.org/.

This page is a bit confusing. Stay cool. You can detect a field with the header Downloads and a hint to the latest version. In this case it shows 3.7.3. Click this.

Python Home Page for Downloads

The next page is completely confusing. The interesting part of this page is somewhere in the middle listing the different download files:

List of download files

We are interested in the 32-Bit version although I am working on a 64-Bit machine. For the learning the 32-Bit version is more flexibel and completely sufficient.

If you click the windows installer for python (second line from below) then a download notice will be shown asking for confirmation:

Confirm download of python installer

Select ‘datei speichern’ (download file) and the installer will be  loaded in your download directory.

In the list of files you can also see   a colum with the MD5-Checksum. This is an offer to check whether the file you have downloaded is indeed not corrupted in some way (viruses etc.) To use the MD5-Checksum code You need a MD5-software tool. I found a good description at this site: https://www.it-administrator.de/downloads/software/197801/, which points you to the following product page: https://www.novirusthanks.org/products/md5-checksum-tool/.

This looks quite OK. But if you want to apply it you have to surround some difficulties when you try to search for your file to check with the directory browser. In my case it shows the directory of the super-user first. To get my python download file I have to select the main drive C:\, there I have to select from Users my actual role as user gdh, and then I can find my download folder with the python exe file to be checked. From the web site with the files list I have copied the MD5-Checksum:

MD5-checksum tool at work

Luckily the check looks good. This encourages to install python 3.7.3 by clicking on the python installer.

Python Installer Window

Because I have already installe python I can only modify it or repair or uninstall. If you click it the first time you will be able to select the option Install. In that case it will propose a directory and it will install python. Before You say Yes You can also click the flag ‘Set Path Variable‘. I did this but the interesting point is that it had no effect when using the power shell (see below). Only when I added the path again with the aid of the powershell it took effect.

USING THE POWERSHELL

To get the powershell you have to press the windows button, then a list of Apps (programs) appears. Under the letter ‘W’ you see the windows powershell. Clicking this topic you will detect several versions of the power shell: 64-Bit and 32-Bit (indicated by (x86)), and the old standard version as well as a more enhanced version called integrated script environment (ISE).

I have started with the old, simple standard version. By right-clicking with your mouse you can add the option to attach the powershell to the task bar which makes it easy to call it up again later.

Section of my task bar including icons for normal powershell (left) and integrated script environment (right)

If you click on the powershell icon the powershell window will open:

Powershell window with the command python entered

If you would confirm this command by pressing the enter key — and you would not have set the path variable with the path of the python executing modul python.exe before — then there would appear a message that the command ‘python’ is not known (I cannot demonstrate it here because I have set the path variable on account of this message meanwhile).

There exists a workaround without setting the path variable explicitly by give the whole path of the python modul, like this:

Powershell calling python with explicit path

You see, this works fine. To make life easier I have set the environment path variable with this python execution path. Then you can enter the command python alone and a python environement opens up.

But, wait a moment. Setting the path variable with the editor for the environment variable alone (see above) this has no effect for the powershell! Also the powershell can show the content of the path-variable correctly:

Powershell showing content of path-variable

(This is the version of the path variable after I have added the path with the powershell too!)

To add the path variable for the python modul explicitly in the powershell you can enter the following  command:

PS C:\Users\gdh> $Env:PATH +=”;C:\Users\gdh\AppData\Local\Programs\Python\Python37-32″

When I did this everything worked fine. Don’t ask, why it didn’t work before with the editing of the environment variable PATH alone.  This is at a first glance like ‘Software mystik’. With much more time for research there exists perhaps an explanation. My first guess would be, that the communication between the powershell and the environment variable has some hidden factors. For more very detailed explanations about the powershell and the editing of the environment variable you can find a good document from microsoft here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-6.

PROGRAMMING PYTHON

For first programming steps look to the first version of this post beginning at the header FIRST PROGRAMMING STEPS somewhere in the middle of the page.

More coding will follow soon.

STARTING WITH PYTHON 3 – The very beginning

eJournal: uffmm.org,
ISSN 2567-6458, July 1, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

The idea is to give some advice to start with python 3 programming. In other posts I have used an integrated package using WinPython including the integrated  development package  spyder. While such an integrated package offers lots of nice tools it hides the basic structures of your system and of the language. Therefore I decided to start again at a more basic level using only the python language and the windows command shell (I myself are working with an ubuntu system for the normal python programming and for the work with a python web-framework called ‘django’). Because I myself am not too much experienced with the windows 10 environment it challenges me to investigate the win10 environment to some degree.

GETTING STARTED FROM SCRATCH: DOWNLOAD PYTHON

To get a fresh start from scratch I have removed all python stuff from my machine.  Then I have downloadad the newest pythion version for windows from https://www.python.org/. This is python version 3.7.3 in the 32-Bit version. There exists also a 64-Bit version, but because not all the different modules are already prepared for the 64-Bit version the 32-Bit version is more flexible. To learn python this is more than enough. To switch to a 64-Bit version would be simple.

LOCAL WINDOWS ENVIRONMENT

On my machine the installation path is:

C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32

To test whether python works on your windows machine you can activate the WindowsPowerShell. You will find this shell by klicking on the Windows Icon in the left corner of your laptown whic opens the list of all Windows-Apps (programs).  At the end of this list there are some windows-Apps including the Windows-Power-Shell. I took the first one. By clicking on the right mouse-button I selected to attach the power-shell on the task bar. This allows me to click once on the icon of the power shell and the power shell will open.

Typing the command ‘python’ the first time into the command line will cause an error message, because Windows does not know where to find the command ‘python’. You can do two things: (i) enter the whole path for python or (ii) inform the PATH-variable about the python path.

Enter the whole path worked out:

PS C:\Users\gerd_2> C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32\python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>

Because this procedure is a bit cumbersom I tried the other proposal, to edit the PATH-variable. One proposal (from the community) goes like this:

  1. Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
  2. Click the “Environment Variables…” button.
  3. Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
  4. The “Edit environment variable” UI will appear.

I could edit the PATH-variable this way, but ist showed no effect. Therefore I tried another procedure, where one can modify the PATH-variable directly from the power-shell. The needed command has the following format:

$Env:PATH += “;Wanted Path”

This addresses the PATH variable, let the actual content as it is and adds after a semicolon the needed path. With the command

$Env:PATH

you can check the actual values of the variable PATH and after adding your new path

PS C:\Users\gerd_2> $Env:PATH +=”;C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32″

youcan check again. It worked, I got my new path added to  the variable. But, even more important, now it worked. Enterin in the power shell only ‘python’ it worked:

PS C:\Users\gerd_2> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

I also tried the ‘help-option’ like this:

>>> help()

Welcome to Python 3.7’s help utility!

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type “quit”.

To get a list of available modules, keywords, symbols, or topics, type

“modules”, “keywords”, “symbols”, or “topics”. Each module also comes

with a one-line summary of what it does; to list the modules whose name

or summary contain a given string such as “spam”, type “modules spam”.

I tried e.g.  the ‘modules option:

help> modules

Please wait a moment while I gather a list of all available modules…

__future__ _tracemalloc glob secrets

_abc _warnings gzip select

_ast _weakref hashlib selectors

_asyncio _weakrefset heapq setuptools

From this I selected the  ‘socket’-option:

help> socket

Help on module socket:

NAME

socket

DESCRIPTION

This module provides socket operations and some related functions.

On Unix, it supports IP (Internet Protocol) and Unix domain sockets.

On other systems, it only supports IP. Functions specific for a

socket are available as methods of the socket object.

Functions:

socket() — create a new socket object

socketpair() — create a pair of new socket objects [*]

fromfd() — create a socket object from an open file descriptor [*]

fromshare() — create a socket object from data received from socket.share() [*]

Thus this function works and looks promising.

FIRST PROGRAMMING STEPS

The main idea of using python is to be able to write some program code which then the machine can run. The basic processing steps are

  1. Start python in the power shell by typing  ‘python’
  2. Enter some python code you want to test.
  3. If you want to edit many python commands together then take a text editor and write some text which looks like python code.
  4. The text you enter directly into a python command line or a edited text in a file which you can load both will be handed out to the python interpreter which translate these pyton commands into a byte code which in turn will be run by the python virtual machine (PVM).

It is helpful befor you start programming to generate at least one folder where you will store your python programs. Following the proposal from Mark Lutz (see below references) I generate in my main directory a folder called ‘code’:

PS C:\> cd $HOME

PS C:\Users\gerd_2> mkdir code

Verzeichnis: C:\Users\gerd_2

Mode LastWriteTime Length Name

—- ————- —— —-

d—– 01.07.2019 18:40 code

To edit a first simple program with name ‘script1.py’ I have called the notepad editor in the power shell like this:

PS C:\Users\gerd_2\code> notepad script1.py

In the editor I have typed the following simple text:

# FILE: script1.py

import sys

print(sys.platform)

print(2**100)

x=’python’

print(x*8)

Then I have stored this file in the ‘code’ folder. Wih the command ‘dir’ one can have a look into the ‘code’ folder:

PS C:\Users\gerd_2\code> dir

Verzeichnis: C:\Users\gerd_2\code

Mode LastWriteTime Length Name

—- ————- —— —-

-a—- 01.07.2019 18:41 92 script1.py

To check what happens when I load this script with the python interpreter one can enter the following command:

PS C:\Users\gerd_2\code> python script1.py

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

STREAM REDIRECTION

One can also redirect the output of the python interpreter from the console into a file:

PS C:\Users\gerd_2\code> python script1.py >savesrc.txt

Then one can again call an editor like notepad to read the content of this file:

PS C:\Users\gerd_2\code> notepad savesrc.txt

CONTENT savesrc.txt:

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

MODULES

Like many other programming language python organizes larger programs by putting together  smaller programs like building blocks. These blocks are called modules and this strategy  can only work if one follows some rules. Mark Lutz formulates it as follows:

“… a module is mostly just a package of variable names, known as a namespace, and the names within that package are called attributes. An attribute is simply a variable name that is attached to a specific object (like a module).” (Lutz, Mark. Learning Python (S.70). O’Reilly Media. Kindle-Version)

One possibility to fetch a module is to use the import command, either directly in a python console or indirectly as command in a python program text. Thus with going direct to the console:

PS C:\Users\gerd_2\code> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> os.getcwd()
‘C:\\Users\\gerd_2\\code’

Here the module ‘os’ has been imported and within tis module exists a function ‘getcwd()’ which fetches the actual path you are in. In my case my home directory and there the folder ‘code’. Doing an import with the new file ‘script1.py’ it works like this:

PS C:\Users\gerd_2\code> python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import script1

win32

1267650600228229401496703205376

pythonpythonpythonpythonpythonpythonpythonpython

Thus the import also causes a run of the script.

If one wants to know which kinds of names are used in the module one can use the command dir():

>>> dir(script1)

[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘sys’, ‘x’]

TO BE CONTINUED…

Go back to the main page.

SOME HELPFUL REFERENCES

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/