I've been using python3 for a long time, but it's a new development, and I wrote it in 3 because it was developed in python2, but in the end, I needed to unify it, so I investigated it. ..
cd 3to2-x.x.x
python setup.py install
You can check the Option by typing the following command in the terminal
3to2 --help
Usage: 3to2 [options] file|dir ...
Options:
-h, --help show this help message and exit
-d, --doctests_only Fix up doctests only
-f FIX, --fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSES
Run 3to2 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a fixer from being run.
-l, --list-fixes List available transformations (fixes/fix_*.py)
-v, --verbose More verbose logging
-w, --write Write back modified files
-n, --nobackups Don't write backups for modified files.
--no-diffs Don't show diffs of the refactoring
For the time being, I decided to write directly using -w
(cutting Branch). Otherwise, only the part that changes from 3 to 2 will be displayed.
3to2 -w aaaa.py
Run and succeed!
# Other
However, this alone may still cause an error.
## 1. Character code related
SyntaxError: Non-ASCII character '\xe6' in file aaaa.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
In this case, write `# coding = <encoding name>` on the first line according to the support of URL.
In the Encoding name part, write ʻutf-8`
## 2. All Strings are preceded by ʻu`, which causes an error
TypeError: "delimiter" must be string, not unicode
For example, if you convert a pandas dataframe:
Before conversion (python3)
`df.to_csv('aaa.tsv', sep='\t')`
After conversion (python2)
`df.to_csv(u'aaa.tsv', sep=u'\t')`
And it says `TypeError:" delimiter "must be string, not unicode`, so
Remove ʻu` before sep ='\ t'
## 3. Similarly, a mysterious error appears in the `to_csv` part.
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
In Python2, it seems that csv does not support unicode, so
Add ʻencoding ='utf-8'`,
df.to_csv('aaa.tsv', sep='\t', encoding='utf-8')
If so, it will be solved
## 4. cld2 can no longer be installed
After making python 2.7.6
`pip intall cld2` fails with the error` No package'libffi' found`
For mac
Install with `brew install libffi`. → Success
I'm not sure because I haven't done it for Ubuntu, but is it okay to use ʻapt-get install libffi` or `libffi-dev`? .. ??
## 5. Unicode-related mystery error 2
TypeError: initializer for ctype 'char *' must be a str or list or tuple, not unicode
The one passed to cld2 above was unicode, so change it as follows
Change before
reliable, _, lang = cld2.detect(name)
After change
reliable, _, lang = cld2.detect(name.encode('utf-8'))
# Conclusion from the error
Python2 has a lot of errors in Unicode, so be careful!
# bonus
## version control
For those who use both Python 2 and 3, it may be convenient to use `pyenv` and separate it by Local.
### Check which version you have
pyenv versions
### Install the required version
pyenv install 2.7.10
### Set locally
pyenv local 2.7.10
This will create a file called .python-verion with the version inside. If you delete this file, you can use the global python version.
## Note the 3to2 execution Python version
I installed python3, so it is said that there is no `3to2` command in the place where I changed to local version python2 with pyenv. So, when changing multiple files, you need to think about whether to execute `3to2` in python3 and then switch to python2 or install both python3 and 2.
Recommended Posts