Koichi Hori

Home About


Using Python on Windows

(Diary of an Old AI Researcher who is still Programming)

9 March 2019

I usually use only mac.
However, I began to feel that even a MacBook Air is heavy since I was about 62 years old.
So, I have bought a light Windows laptop FMV U75B (748g) in 2018 .

At first, I used this Windows laptop only for presentation, mail, and office document processing. (For presentation, I presented html files on Windows, which were written by my own program on mac. I do not use PowerPoint, because I do not like PowerPoint.)
Well, but, I got too impatient to modify and test my programs during my business trip, many times. :-)
Should I throw away the Windows laptop and return to the MacBook Air?
My colleague said returning to MacBook Air was the best choice, but I determined to install Python on my Windows laptop, because I did not want to throw away the laptop I just bought.

Following is a note of a few tips to use Python on Windows.

Installing Python on Windows

First of all, installing Python3 on Windows is easy.
We can download the installation package from
https://www.python.org/downloads/windows/ .

I could install the Python3.7 without any problems on my Windows laptop.

Running Python on Windows

Note that the command to run Python3 is not 'python3' but 'python' or 'py' regardless of the installed version, on Windows.

Reading and writing unicode files by Python on Windows

Reading or writing files including unicode multi-byte characters causes 'UnicodeDecodeError', which does not happen on mac.
To fix this problem, we should add 'encoding="utf-8"' when we open the files. For example,

open(filename, "r", encoding="utf-8")

Using subprocess.run on Windows

I used, in my program, subprocess.run to copy a file to another, i.e.

import subprocess
subprocess.run(["cp", '/Users/hori/mysystem/templatefile.html', myworkingfile])

I must change "cp" to "copy", since this is Window OS, of course.
Moreover, I must change the slash into backslash. That's crazy, isn't it? But this is Windows, of course.
The code,

subprocess.run(["copy", '\Users\hori\mysystem\templatefile.html', myworkingfile])

does not work, either.
We must escape the backslash by putting another backslash like '\\Users\\hori...'.
Instead, to make the code simpler, we can use the raw string of Python (r + string), and the code is

subprocess.run(["copy",r'\Users\hori\mysystem\templatefile.html', myworkingfile])

OK, finally, my Python program began to run on Windows laptop, too.

(We need not replace slash by backslash in the file name when using "open". E.g., the code:

open('/Users/hori/mysystem/templatefile.html', "r", encoding="utf-8")

works, because Python deals with the OS dependent file path syntax.)

To the extent possible under law, the person who associated CC0 with this work has waived all copyright and related or neighboring rights to this work.

Related entries (automatically calculated):
Using unicode characters in Windows command line
Redirecting URL in Ruby on Rails
Unicode decode error "'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte" when using MeCab
css <pre> and <code> for mobile devices
Aligning Facebook button and Twitter button
Login window freezes when making VNC connection from Windows to Mac
Showing the favicon in Google search results
On This Day: Atomic Bomb Dropped on Nagasaki
Mechanical engineers and electrical engineers have different mental models of oscillation
UAV/UGV Autonomous Cooperation
A small program which extracts rhythmic word sequences such as Tanka(57577) or Haiku(575) from a plain text
UNESCO: `Do you know AI or AI knows you better? Thinking Ethics of AI'
AI (Artificial Intelligence) and Philosophy
Koichi Hori: Last Lecture
Toward AI-embedded Society where AI is Not Recognized as AI
What an old AI researcher thinks after watching the movie "Green Book" - about Racism, Discrimination, and AI (Artificial Intelligence)
Culture as the base of our country: Prof. Inose
AI support for Ethical AI Design
Difference between Science and Engineering
The University of Tokyo Academic Archives Portal - UTokyo Digital Collections
Koichi Hori Top page
Civilization, Culture, Science, and Technology
What is Artificial Intelligence?
Koichi Hori