Home

Beginning Python

 

Some information about how to install Python is presented here. The first part is for Windows and the second for Linux (Ubuntu/CentOS Stream).

 

 

In the case of Windows

 

Python can be installed from the website of Python. But, when you use Python for various purposes, it is better to install Anaconda, on which you can use a Python.

First, I explain how to install Python from the website of Python. Then, installation of anaconda is explained.

 

Installation of Python

 

If you install Python in the default folder, the pip command will not be called (2020.08). To avoid this inconvenience, Python should be installed in a folder created by you.

To create a folder, run File Explorer and open C drive(Figure A.1).

Figure A.1

 

Click on the New folder icon, and rename the new folder as you like. In Figure A.2, the new folder is named MyPython.

Figure A.2

 

Now, you can install Python in the MyPython folder.

Visit the website

 

https://www.python.org/

 

The following site (Figure A.3) appears.

Figure A.3

 

Put the mouse pointer on the gDownloadsh string, then the button for downloading (in Figure A.3, gPython 3.8.5h button) will be presented. Click this button.

The gSave Ash dialog form appears (Figure A.4) (or, something like this on the upper right corner on Windows 10).

Figure A.4

 

Click the gSaveh button. Downloading starts and the process is being displayed on the lower left corner (Figure A.5) (or in the upper right region on Windows 10.

Figure A.5

 

After downloading finished, the string gOpen fileh is shown on the lower left corner (Figure A.5) (or in the upper right region of Windows 10)

Click on the string gOpen fileh, then the downloaded file is executed and the Python installation form is presented (Figure A.6).

Figure A.6

 

Choose both options gInstall launcerch and gAdd Pythonch, then click on the string gCustomize installation.h

The gOptional Featuresh form will appear (Figure A.7). Select all the options. Of course, you may not need all the options. But, to select all of them is easy way.

Then, click on the gNexth button.

Figure A.7

 

On the next form, you choose the folder in which Python is to be installed (Figure A.8).

Figure A.8

 

Click on the gBrowseh button. The gBrowse For Folderh dialog box will be presented. Choose the folder in which you are installing Python. In Figure A.8, the folder gMyPythonh, which is created before downloading Python, is chosen.

Click the gOKh button. The gAdvanced Optionsh form is set as in Figure A.9.

Figure A.9

 

As the customized install location, the folder which is chosen in the dialogue box (Figure A.8) is set.

Click on the gInstallh button, then installation of Python starts.

When the installation is completed, the form shown in Figure A.10 will be shown.

Figure A.10

 

Click the gCloseh button.

After the installation of Python, the Start Menu contains Python-related items (Figure A.11).

Figure A.11

 

Click on the IDLE (Python 3.8 32-bit) item, IDLE (Integrated Development and Learning Environment) starts and the window shown in Figure A.12 appears.

Figure A.12

 

Press down a key on the keyboard, the corresponding character is shown at the location where the cursor is.

The position to input starts after the string g>>>h.

Type in script g1 + 2h, then press the Enter key.

The result is shown as in Figure A.13.

Figure A.13

 

The mode in which scripts are executed one by one as in Figure A.13 is called the interactive mode.

Python programs can be written in a file, which can be executed by the python command. This mode is called the script mode.

Python script files can be developed with the editor of Python IDLE.

Start the IDLE (Figure A.11) and choose the menu item gFile|New Fileh of IDLE shell (Figure A.14).

Figure A.14

 

A window of IDLE editor will be shown (Figure A.15).

Figure A.15

 

First, name the new file by menu item gFile|Save Ash of the new window of IDLE editor (Figure A.15).

Choose menu item gFile|Save Ash, the gSave Ash dialog form is shown (Figure A.16).

Figure A.16

 

Choose the folder in which the file is saved, and set the file name with the filename extension g.pyh

Click on the gSaveh button.

Now, write your script code (Figure A.17).

Figure A.17

 

Save your script in the file by choosing the menu item gFile|Saveh (Figure A.18), before executing the script. The new script must be saved in the file to be executed by Python.

Figure A.18

 

After having saved the file, choose the menu item gRun|Run Moduleh to execute the script (Figure A.19).

Figure A.19

 

When the script is executed, output by print statement is displayed in the Python IDLE window (Figure A.20).

Figure A.20

 

When you want to execute the script file, which has been already developed, open the file by choosing the menu item gFile|Openh in Figure A.14.

To execute the opened script file, choose the menu item gRun|Run Moduleh of the window, in which the file is opened (see Figure A.19).

 

 

Installation of Modules (Libraries)

 

Modules can be installed by the pip command, which is executed in a terminal window.

Since installation may require administrator privileges, run the terminal soft as Administrator.

Click on the menu item gCommand Prompth on the Start Menu by the right button. Then choose gMoreh, then gRun as administratorh (Figure A.21).

Figure A.21

 

When the terminal soft is called by Administrator, the string gAdministratorh is displayed in the title of the window (Figure A.22).

Figure A.22

 

Modules can be installed by the command pip as follows

 

pip install list-of-module(s) 

 

In Figure A.23, the pip command installs modules numpy, scipy, pandas, matplotlib, seaborn and xlrd.

Figure A.23

 

Execute the command in Figure A.23, then installation starts and ends as shown in Figure A.24.

Figure A.24

 

In the case of Figure A.24, the message gWARNING:cYou should consider upgradingch is displayed in the end.

Type in the command to upgrade gpiph as suggested, then execute it.

When the upgraded gpiph is successfully installed, the message is shown as in Figure A.25.

Figure A.25

 

 

You would find some sample scripts in Python in the following websites:

 

http://y-okamoto-psy1949.la.coocan.jp/Python/en/

http://y-okamoto-psy1949.la.coocan.jp/Python/en1/

 

 

Installing Anaconda and PyStan

 

PyStan, the Python interface to Stan, requires C++ compiler for Stan scripts. The C++ compiler must be selected so that it compiles Stan script without errors. If we use Anaconda to install PyStan, the selection of the C++ compiler is automatically done by Anaconda and installed with PyStan.  Of course, if you do not use pystan, you do not have to install it.

Installation of Anaconda is easy as shown below:

 

Anaconda can be down loaded from the website:

 

https://www.anaconda.com/products/individual

 

Visit the above website, and click on the gDownloadh button (Figure B.1).

Figure B.1

 

The form gAnaconda Installersh appears (Figure B.2).

Figure B.2

 

When you install Anaconda in Windows, click on the string g64-Bit Graphical Installers (466 MB)h under the string gWindowsh.

In the case of the browser Edge, the gSave Ash dialog window is presented (Figure B.3) (or someone like this on the upper right corner on Windows 10).

Figure B.3

 

Click on the gSaveh button, then downloading starts.

When downloading finishes, the string gOpen fileh is presented at the lower left corner of the window (Figure B.4) (or the message is presented in the upper right region on Windows 10).

Figure B.4

 

Click on the string gOpen fileh, then execution of the downloaded file starts and the form of Anaconda setup is presented (Figure B.5).

Figure B.5

 

Click on the gNexth button.

The gLicense Agreementh dialog form appears (Figure B.6).

Figure B.6

 

Click on the gI Agreeh button.

The gSelect Installation Typeh dialog form appears (Figure B.7).

Figure B.7

 

Choose the gJust Meh option, then click on the gNexth button.

The gChoose Installation Locationh dialog form is presented (Figure B.8).

Figure B.8

 

Click on the gNexth button (Figure B.8).

The gAdvanced Installation Optionsh dialog form is presented (Figure B.9).

Figure B.9

 

I recommend that both options are not chosen, so that effects on the system by the installation should be minimum.

Click on the gInstallh button.

When the installation finishes, the gInstallation Completeh form is presented (Figure B.10).

Figure B.10

 

Click on the gNexth button.

The form like in Figure B.11 is presented.

Figure B.11

 

Click on the gNexth button, then the gCompleting Anaconda Setuph form is presented (Figure B.12).

Figure B.12

 

If you do not read gtipsh and gresourcesh for now, remove checkmarks for them.

Click on the gFinishh button (Figure B.12).

 

After the installation of Anaconda, menu items of Anaconda are shown in Start Menu (Figure B.13).

Figure B.13

 

When you choose the menu item gAnaconda Powershell Prompth, the terminal soft is called (Figure B.14).

Figure B.14

 

The stringh(base)h indicates that this terminal is running in the virtual environment gbaseh. The virtual environment gbaseh has been created automatically when Anaconda is installed.

In this terminal, commands for the virtual environment can be called. Commands not for the virtual envirenmnet, e.g., cd, ls, and so on, can be also called.

Modules for Python can be installed by the conda command, although the pip command can be also used. In a virtual environment, usage of the conda command is recommended.

To install modules, the conda command is called in the following format:

 

conda install module-list

 

Names in the module-list should be separated by blank characters. Notice that modules are installed on the current virtual environment, that is, modules must be installed on the virtual environment in that they are used.

To install modules numpy, scipy, pandas, matplotlib, seaborn and xlrd, type in the code as in Figure B.15.

Figure B.15

 

 

PyStan on Windows

At present (2023.02.06), on Windows PyStan 2.17.1.0 can be used in a Pyrhon 3.7 virtual environment

A Python 3.7 virtual environment can be created by the conda command.

 

conda create n py37 python=3.7

 

Then, to enter the environment, run the command

 

conda activate Py37

 

In the Python 3.7 virtual environment, install PyStan 2.17.1.0

 

conda install pystan==2.17.1.0

 

Notice that equal sign = is doubled like ==.

 

When you run the script, which uses pystan, parameter value n_jobs for MCMC samplig must be 1, 

Notice that

 

 

In the case of Linux (Ubuntu/CentOS Stream)

 

I recommend to use Python in a virtual environment of Anaconda in the case of Linux.

The installation file of Anaconda can be downloaded from the website

 

https://www.anaconda.com/products/individual

 

Run the file by the code like this

 

sh ./Anaconda3-2018.12-Linux-x86_64.sh

 

File name Anacondaxxx is depend on the time when the file is downloaded.

 

In installing, the following message will be presented.

 

Do you wish the installer to initialize Anaconda3

in your /home/user1/.bashrc ? [yes|no]

 

I recommend to choose eyesf, because this simplify the installation.

After the installation, restart the Linux.

After the restart, head lines in your terminal window start with string e(base)f, which means that the terminal is in a virtual environment named base.

In this terminal, you can use commands for a virtual environment, and commands on Linux, e.g., cd, ls and so on as before.

To update Anaconda, run the following codes

 

conda update anaconda

 

and

 

conda update --all

 

 

 

To get out of the virtual environment, execute the code

 

conda deactivate

 

When you are out of a virtual environment, head lines start without a virtual environment name.

To run a Python script file, e.g., hello.py in the folder Pythonfiles, run the code in the folder Pythonfiles (Figure L.1)

 

python hello.py

 

Figure L.1

 

When you install a module, e.g. pystan, run the code

 

conda install pystan

 

PyStan is installed PyStan 2.19 for a Python 3.19 or older version, For Python 3.10, PyStan 3 would be installed. For information about PyStan 3 and Python 10, check this website.

 

 

 

Home