Setting up the Python environment
- To setup the Python programming environment, you need to do following steps:
- Install pyenv
- Install Python
- Windows and Mac users go different route for step1, while step 2 is common.
Install
Installing pyenv
Windows
Install WSL2 (Windows service for linux ver.2); following is an example in Windows 10
- Open "Windows Power Shell" or "Terminal" as Administrator.
- Type
wsl --set-default-version 2
(for safe) - Type
wsl --install -d Ubuntu
. - After installation is done, open
Ubuntu
in Application.
When above doesn't work, check "Windows の機能の有効化、または無効化", then "Linux 用 Windows サブシステム", "仮想マシンプラットフォーム" is ON.
- Note that WSL makes home directory (
/home/your_name
) which is different from the Windows user directory (C:\Users\your_name
). - You can access Windows system from Ubuntu like:
cd
to Desktop bycd /mnt/c/Users/your_name/Desktop/
. It is useful to make symbolic link between Ubuntu and Windows. So, in Ubuntu terminal (not PowerShell), do
cd ln -s /mnt/c/Users/your_user_name/Desktop/ desktop
If some commands are unavailable (e.g. vi), install them by
sudo apt install vim
.Install dependencies
sudo apt update sudo apt -y upgrade
then
sudo apt -y install build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev curl \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Install pyenv
curl https://pyenv.run | bash
Mac
- Install Homebrew (if not installed)
- copy command in https://brew.sh/ja/ in the terminal.
- Install pyenv via Homebrew
brew install pyenv
pyenv configuration
- Update Bash Configuration. Execute following three lines.
echo 'export PYENV_ROOT="HOME/.pyenv"′>> /.bashrcecho′exportPATH="HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="HOME/.pyenv"′>> /.bashrcecho′exportPATH="PYENV_ROOT/bin:PATH"′>> /.bashrcecho′eval"PATH"' >> ~/.bashrc echo 'eval "PATH"′>> /.bashrcecho′eval"(pyenv init -)"' >> ~/.bashrc
if you are using zsh, replace
~/.bashrc
with~/.zshrc
.Reload Bash Configuration
source ~/.bashrc
- If you are using zsh, replace
~/.bashrc
with~/.zshrc
.
Install Python using pyenv
List available Python versions
pyenv install --list
Choose a version and install it (e.g., Python 3.9.5)
pyenv install 3.9.5
It will take some time ...
Set Global Python Version
pyenv global 3.9.5
Verify Installation
python --version
Installing pip
pip
is the Python package manager.- If pyenv is installed, pip is automatically installed.
- To not installed, do the following commands to install (both Windows WSL2 and MacOS).
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
- After installing pip, you can download the library like:
pip install numpy
.
Python install from source
- When you don't have permission or OS is old, installing Python via pyenv does'nt work.
- In that case, you need to insall Python from source code.
- Make some directory and go there:
mkdir python
,cd python
. - Get the tar.xz file from web, using
wget
. tar jxvf Python.x.x.x.tar.xz
./configure --prefix=/your_home/python
.make
thenmake install
.pip
is also install. Do not forget to set PATH, and alias to mappython
->python3
andpip
->pip3
.
Troubleshooting
'ImportError: No module named '_tkinter', please install the python3-tk package'
(Windows)- Do
sudo apt install python3-tk
.
- Do
VScode
- Visual Studio code (VS code) is a free code editor made by Microsoft.
- Works both on Windows and Mac.
Setup
- Download the installer from https://code.visualstudio.com/
- Install extensions(拡張機能)
- "Japanese Language Pack" (if you want).
- "Python"
Wrirting Python script
- Open a folder where Python script exists
- After writing the code, save file
The "run" command (">" button) to run the script
You can change the Python interpreter (like pyenv) by clicking the "Python ..." in the bottom bar.
Google Colaboratory
- Google Colab is a free, cloud-based platform where you can write and run Python code using Jupyter notebooks
- You can use expensive GPUs (graphical proceccing unit) with Google Cloab for free (with some limitations)
Setup
- Sign in with your Google Account (if exists).
- Go to Google Colaboratory page in your web browser.
- Create a New Notebook
- "File(ファイル)" --> "New Notebook(ノートブックを新規作成)" to start a new Python notebook.
- Copy your notebook to Google Drive.
- Keep your notebook to Google Drive by clicking "Copy to Drive(ドライブにコピー)".
- Use the Notebook
- "+Code(コード)" will insert the code cell in your notebook.
- In a code cell, you can input Python code.
- With ">" button, the code will be execured.
- Saving and Sharing
- "File" --> "Save" will save your notebook to Google Drive.
- Click the folder button in left panel then folders are open.
- You file is save to "drive" --> "Mydrive" (by default).
Notebook-specific grammar
- Putting
!
indicates the shell command. - Putting
%
indicates the magic command.- e.g. To change the directory, specify
%cd
(not!cd
because it generates new shell).
- e.g. To change the directory, specify
- You can directly edit the Python script (.py) file in Google Colab; click the .py file from the directory tree.
Using GPU
- "Runtime" tab.
- "Change the type of Runtime".
- Choose GPU you want.