Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)

A memorandum of migration (coexistence) from Python 2 to Python 3 on Windows

Thing you want to do

  1. Migrate from Python 2 to Python 3 It's about time I wanted to use Python 3 mainly, so I decided to introduce it
  2. I want to be able to use Python2 as well (coexistence) It is troublesome to rewrite all the code I have written so far, so I want to be able to use Python 2.7 as well Achieve coexistence of Python3 and Python2 by building a virtual environment of python2.7 virtual environment using conda (described later)
  3. Development environment (Jupyter, PyCharm settings) Allows you to select python2, 3 in your usual Jupyter, PyCharm

1. Migration from Python 2 to Python 3

1.1 Uninstall Python2

Uninstall Python 2.7 from Program Changes / Removes

1.2 Installing Python 3

Introduced in Anaconda Go to the Anaconda site (https://www.continuum.io/downloads) and download the latest Anaconda (Python3) installer Run the installer

image.png

Select a place to put Anaconda

You will be asked if you want to add a PATH, so select "Yes" this time. If you do not select this you will have to add your own PATH The second checkbox is also "Yes" image.png

Successful installation of Python 3

2. Python2 environment construction

Build a virtual environment for Pyrhon2 in Anaconda3 using conda Execute the following at the command prompt Packages can be added later

# conda create --name [Virtual environment name] python=2.7(Favorite version) [package] ... [package]
conda create --name python27 python=2.7 numpy scipy pandas jupyter

#This time python2.I put 7 alone and added a package, but it is also possible to put anaconda
conda create -n anaconda2 python=2.7 anaconda

There should be a folder with the specified environment name in the ~ Anaconda3 / envs directory. image.png

Getting in and out of the virtual environment

#Enter the virtual environment
# activate [Virtual environment name] 
acitvate python27
#Get out of the virtual environment
# deactivate [Virtual environment name]
deactivate python27

Package management

Packages are not common in Python3 and Python2 environments, so you need to put the package you want to use in each environment.

You can enter the Python3 environment with conda or pip as usual To enter the Python2 virtual environment, enable the virtual environment once and then use conda or pip.

activate python27;conda install numpy
activate python27;pip install numpy

conda install --name python27 numpy #In the case of conda, you can also specify the environment name and enter it like this
pip install --name python27 numpy #This cannot be done ×

3. Development environment

Allows you to select both Python 2 and Python 3 in your usual development environment 3.1 Jupyter As it is, even if you start jupyter that you put in Anaconda3, you can only select Python3 image.png

You need to do the work of adding a kernel

#First enable the virtual environment
activate python27
#You can add kernel with the following command
ipython kernel install --user --name=python27 --display-name=Python2

--name: environment name --display-name: The name displayed by jupyter

The added kernel resides in ~ AppData \ Roaming \ jupyter \ kernels If you want to remove a kernel, just remove it from this folder

This makes it possible to select Python 2 from juypter as well. image.png

3.2 PyCharm

Select File → Setting → Project Interpreter Select the part indicated by the red circle Select Add Local image.png

Select the virtual environment python.exe you just entered Anaconda3\envs\python27\python.exe Apply image.png

Now you can also select Python2 in PyCharm

reference

Building a python environment for those who aim to become data scientists 2016 http://qiita.com/y__sama/items/5b62d31cb7e6ed50f02c Settings for easy selection of multiple kernels in Jupyter http://qiita.com/tomochiii/items/8b937f15c79a0c3eae0e

Recommended Posts

Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Add a Python virtual environment to VSCode
From installing Ansible to building a Python environment in Vagrant's virtual environment
How to create a Python virtual environment (venv)
From Python environment construction to virtual environment construction with anaconda
Python: Creating a virtual environment (venv), starting and stopping
Build a python virtual environment with virtualenv and virtualenvwrapper
Building and enabling a python virtual environment, etc. (venv)
Build a python virtual environment with virtualenv and virtualenvwrapper
Building a Python virtual environment
Building a Python virtual environment
How to build a new python virtual environment on Ubuntu
Summary from building Python 3.4. * From source to building a scientific computing environment
How to develop in a virtual environment of Python [Memo]
Build a Python environment and transfer data to the server
Overview of Python virtual environment and how to create it
Building a virtual environment with Python 3
About Python, from and import, as
From building a Python environment for inexperienced people to Hello world
Everything from building a Python environment to running it on Windows
Building a Python development environment on Windows -From installing Anaconda to linking Atom and Jupyter Notebook-
From Python to using MeCab (and CaboCha)
Python virtual environment and packages on Ubuntu
Send a message from Python to Slack
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
I want to build a Python environment
[Mac] Building a virtual environment for Python
Creating a python virtual environment on Windows
Porting and modifying doublet-solver from python2 to python3.
Build a python virtual environment with pyenv
How to use is and == in Python
Building a virtual environment for Mayavi dedicated to Python 3.6, Anaconda, Spyder users
[Introduction to Python] What is the difference between a list and a tuple?
How to install python package in local environment as a general user
Connect to postgreSQL from Python and use stored procedures in a loop.
Read CSV file with Python and convert it to DataFrame as it is
How to input a character string in Python and output it as it is or in the opposite direction.
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
How to share a virtual environment [About requirements.txt]
How to open a web browser from python
Ubuntu18.04.05 Creating a python virtual environment in LTS
Building a python environment with virtualenv and direnv
python Binary search It is surprisingly easy to implement bisect.bisect_left and bisect.bisect_right from 0
ffmpeg-Build a python environment and split the video
Create a virtual environment with conda in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
When I tried to create a virtual environment with Python, it didn't work
How to generate a Python object from JSON
Data retrieval from MacNote3 and migration to Write
Prepare a development environment that is portable and easy to duplicate without polluting the environment with Python embeddable (Windows)
Create a Django project and application in a Python virtual environment and start the server
A complete guidebook to using pyenv, pip and python in an offline environment
[Python] Web development preparation (building a virtual environment)
Build a simple Python virtual environment without pyenv
Build a virtual environment with pyenv and venv
Build a python environment to learn the theory and implementation of deep learning
Note: Start Anaconda, enter the Python virtual environment, and connect locally to MongoDB.
[Venv] Create a python virtual environment on Ubuntu
WEB scraping with python and try to make a word cloud from reviews