I tried changing the python script from 2.7.11 to 3.6.0 on windows10

It was a concern that my home script was running on python2.7, so I tried python3. This year's work is safe, and I've done it, so I'm coding for New Year's Eve.

Prerequisites:

Flow

  1. Identify the target script ――I wonder if it's okay to use only the scripts that you usually use for temporary verification that you don't think you will use in the future.
  2. Install python3.6 on windows -Download and install 3.6.0 from Download Python | Python.org --Checked Add PATH in the installation wizard --If you want to change what you want to refer to from cmd.exe, change the path from 2.7 to 3.6 from the advanced settings of windows.
  3. Change pycharm settings --Create a new project for 3.6
  4. Required package installation --I implemented it from pycharm, but maybe because it became py3, some changes on the windows side were necessary --Notes will be described later
  5. Correct your own code --Move the required script as it is from the old project for py2 and fix and test the error part --Detailed changes will be described later
  6. Execution path correction --Change what was referenced in Task Scheduler and tablacus explorer --The project path has changed because it was a new project.

What kind of processing was done and how was it handled (python2.7.11 → py3.6.0)

Since it is "in my case", it changes on a case-by-case basis depending on the writing style and the functions used, and it is not a story that I made it a good writing style as python3 at the level that it worked without problems.

Each function With or without code change Remarks
Read file line by line Change a_file.xreadlines→a_file.To readlines(See below)
File system reference(ls -R-like) As it is os.path and os.Use listdir
Japanese string operation Change Encoding specification is deleted(See below)
Receive Japanese arguments Change Encoding specification is deleted(See below)
String output Change Famous guy(See below)
String replacement Change Encoding specification is deleted(See below)
String formatting As it is +Character string concatenation use
Text file output As it is codec.open use
bottle(web server) As it is Reinsert the bottle
Image size acquisition with PIL As it is I put the pillow back in, but it's a little troublesome(See below)
Get median with numpy As it is I reinserted numpy, but it's a little troublesome(See below)
sleep at time As it is
ionice with psutil As it is reinsert psutil
Get current time As it is
System command execution on os As it is However, if you change the encoding by exchanging arguments, you do not need to specify it.
Dialog display(GUI) Change Use MessageBoxA of win32api of ctypes(See below)

Details of changes such as code correction

print statement

The one you often see.

# before
print "hoge"

#after
print("hoge")

Character code encoding

I don't need mysterious spells anymore.

# before
import sys
reload(sys)
sys.setdefaultencoding("cp932")

unicode(hoge_string, encoding='cp932')

#after
#I don't need spells anymore!However, if you write it, the way of writing the first two lines will change
import importlib
importlib.reload(sys)
sys.setdefaultencoding('cp932')

#You don't even need to specify each string variable
hoge_string

Message box display when using win32api

Use MessageBoxW instead of MessageBoxA. Reference: Call MessageBox (Windows API) from Python 3

# before
def win_alert(mes):
    user32 = windll.user32
    user32.MessageBoxA(
        0,
        mes,
        "info",
        0x00000040)

#after
def win_alert(mes):
    user32 = windll.user32
    user32.MessageBoxW(
        0,
        mes,
        "info",
        0x00000040)

xreadlines I couldn't find the official docs but xreadlines seems to be obsolete in python3. However, since there are readlines, I took x.

# before
for line in itr.xreadlines():

#after
for line in itr.readlines():

Reference: [Porting code to Python 3 using 2to3 --Dive Into Python 3 Japanese version](http://diveintopython3-ja.rdy.jp/porting-code-to-python-3-with-2to3. html)

Pillow installation

When I do "pip install pillow" on windows, I get angry that zlib is running during installation. Anyway, I got a pre-built binary and installed it.

#Open the following in your browser"Pillow‑3.4.2‑cp36‑cp36m‑win_amd64.whl"DL
#   http://www.lfd.uci.edu/~gohlke/pythonlibs/
$ pip install Pillow‑3.4.2‑cp36‑cp36m‑win_amd64.whl

Reference: [Can't install Pillow for Python 3.x in Windows --Zlib is required --Stack Overflow](http://stackoverflow.com/questions/38733647/cant-install-pillow-for-python-3-x- in-windows-zlib-is-required) Reference: python --Zlib error when installing Pillow --Stack Overflow Reference: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

numpy installation

According to the error at the time of installation, I installed "Visual C ++ Build Tools" and then re-executed it.

Reference: Download the Visual C ++ Build Tools (standalone C ++ compiler, libraries and tools)

(Bonus) Start python3 bottle from windows task scheduler but hide console

Even when using python2, the bottle web server was automatically started without displaying the console. But somehow pythonw.exe doesn't start the bottle script ... Of course, when you start it with python.exe, it will be displayed on the console ... (I forgot to back up the settings of the task scheduler and how it was realized ...)

So, after a quick search, if you select "Run regardless of whether the user is logged on" in the task scheduler, it will not be displayed. I was able to do it, but why not? タスクスケジューラ.png

#Settings on the task scheduler
[General]→[Security options]
"Run regardless of whether the user is logged on"

[operation]→[program/script]
"C:\hoge\Python36\python.exe"

[operation]→[Add argument]
C:/hoge/bottle_sample.py


Note: [Batch file executed from Task Scheduler in Windows 7 does not appear on the screen. --Microsoft Community](https://answers.microsoft.com/en-us/windows/forum/windows_7-winapps/windows7%E3%81%A7%E3%82%BF%E3%82%B9%E3%82 % AF / ccff2c2f-5a0e-4f3d-ae81-9dff3e36fc3c)

in conclusion

Perhaps because the target was small and only simple functions, it took an hour or two in total even while investigating, so it was relatively easy to move. However, I'm not conscious of how to write it correctly as python3, so I have to study it properly ...

Especially for those who have a hard time in Japanese on windows, it is essential to upgrade to python3. (You don't have to use cp932 or mysterious character code anymore, or worry about the consistency match between encoding, decoding and unicode ())

Most of the libraries are already compatible. (If this is n times faster like ruby or php, it's gabung gabung)

that's all.

Recommended Posts

I tried changing the python script from 2.7.11 to 3.6.0 on windows10
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I ran python on windows
I tried to graph the packages installed in Python
The wall of changing the Django service from Python 2.7 to Python 3
I tried Python on Mac for the first time.
I tried to detect the iris from the camera image
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried python on heroku for the first time
I wanted to use the Python library from MATLAB
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
I tried to notify the honeypot report on LINE
I tried to summarize the string operations of Python
[Python] I tried to get the type name as a string from the type function
I tried to find the entropy of the image with python
I replaced the Windows PowerShell cookbook with a python script.
I tried to simulate how the infection spreads with Python
Python 3.6 on Windows ... and to Xamarin.
I tried to create API list.csv in Python from swagger.yaml
I tried using the Python library from Ruby with PyCall
I tried to touch Python (installation)
I tried running PIFuHD on Windows for the time being
I tried face recognition from the video (OpenCV: python version)
[Python] I tried to visualize the follow relationship of Twitter
How to pass arguments when invoking python script from blender on the command line
I tried to enumerate the differences between java and python
I tried to launch ipython cluster to the minimum on AWS
I tried to divide the file into folders with Python
I tried to get various information from the codeforces API
I tried to move the ball
I tried to estimate the interval.
Update Python on Mac from 2 to 3
I tried to create a Python script to get the value of a cell in Microsoft Excel
I tried to summarize the languages that beginners should learn from now on by purpose
I tried to predict the genre of music from the song title on the Recurrent Neural Network
I tried to solve the ant book beginner's edition with python
I tried to digitize the stamp stamped on paper using OpenCV
I tried to display the video playback time (OpenCV: Python version)
I tried to register a station on the IoT platform "Rimotte"
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
I tried to improve the efficiency of daily work with Python
Everything from building a Python environment to running it on Windows
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
[Kivy] How to install Kivy on Windows [Python]
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I want to use jar from python
Connecting from python to MySQL on CentOS 6.4
I tried using UnityCloudBuild API from Python
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I tried to estimate the pi stochastically
I tried to touch the COTOHA API