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:
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) |
The one you often see.
# before
print "hoge"
#after
print("hoge")
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
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)
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
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)
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?
#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)
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