How to get started with the 2020 Python project (windows wsl and mac standardization)

Install a modern, linux python environment on Windows 10 using the same procedure as on a Mac.

Two years ago, referring to a good article for Mac "How to get started with the Python project in 2018", WSL (Windows Subsystem for) on windows 10 Prepared a modern linux python environment (at that time) on Linux) (Article: Prepare a modern Python environment by hitting a command about 10 lines ). I'm still using the environment after updating it at that time, but since I've been using Mac for scala / java business, I decided to adopt it for building a python environment that is more compatible with the Mac method. .. In conclusion, I thought that this approach was definitely possible considering maintainability. It's a general-purpose approach regardless of language, so even beginners who need to use python and javascript in the near future should take on the challenge even when they have time.

This year's policy

--Introduce homebrew using WSL version ubuntu --Introduce anyenv from homebrew. --Put pyenv from anyenv and change package management from pipenv to poetry.

Using anyenv takes a lot of work, but I wanted to manage the version of nodejs etc. in the same way, so I challenged it. It seems that there is asdf as an option to build a more diverse environment, but I wonder if that will be adopted about two years after it withers.

(Bonus) By the way, make the vscode environment common to win / mac.

Personally, the big change from two years ago is that the ratio of using vscode has increased. ... Before that, I used sublime text because I had to deal with billions of lines of text files. Nowadays, vscode is the editor for both Mac and windows. So I wrote the following article. If you are vscode + python, please take a look here as well. Realize an environment close to Mac easily with Windows 10 (wsl + vscode + homebrew)

Procedure with WSL version ubuntu

There are only a few lines of command differences between Mac and windows wsl (ubuntu). Recently, the window10 machine of my personal computer is not working well and I am about to die, so when I set my personal computer to mac only, I thought I should write the difference on Mac.

① Introduce homebrew

Homebrew can be easily installed in the WSL version of ubuntu. Below, the installation procedure to ubuntu in the initial state

.sh


sudo apt update
sudo apt upgrade
sudo apt install linuxbrew-wrapper build-essential
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> ~/.bashrc
source ~/.bashrc

② Introduce anyenv

From here on, the procedure is almost the same as for Mac. Thank you. Include anyenv and anyenv convenience plugins. Some plugins aren't needed for pyenv, but be prepared for days when you use other languages.

brew install anyenv
anyenv init
echo 'eval "$(anyenv init -)"' >> ~/.bashrc
exec $SHELL -l
anyenv install --init
anyenv -v
mkdir -p $(anyenv root)/plugins
git clone https://github.com/znz/anyenv-update.git $(anyenv root)/plugins/anyenv-update
git clone https://github.com/znz/anyenv-git.git $(anyenv root)/plugins/anyenv-git
anyenv update

③ Introduce pyenv / python / poetry

There are some steps specific to ubuntu for wsl here. However, I just click the following command. If the version of ubuntu changes, the procedure may change. In that case, let's do our best by google with one of the keywords pyenv / ubuntu / poetry. In the article two years ago, the procedure changed slightly about once every six months and I updated the article, so I will check it at the same frequency this time and update this article as appropriate (I feel like I do not write much) ..

.sh


anyenv install pyenv
exec $SHELL -l
sudo apt-get install -y libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
pyenv install 3.8.2
pyenv global 3.8.2
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
source $HOME/.poetry/env

At the end

After installation

It should look like this.

userid@DESKTOP-YourID:~$ poetry
Poetry version 1.0.5

USAGE
  poetry [-h] [-q] [-v [<...>]] [-V] [--ansi] [--no-ansi] [-n] <command> [<arg1>] ... [<argN>]

ARGUMENTS
  <command>              The command to execute
  <arg>                  The arguments of the command

GLOBAL OPTIONS
  -h (--help)            Display this help message
  -q (--quiet)           Do not output any message
  -v (--verbose)         Increase the verbosity of messages: "-v" for normal output, "-vv" for more verbose output and "-vvv" for debug
  -V (--version)         Display this application version
  --ansi                 Force ANSI output
  --no-ansi              Disable ANSI output
  -n (--no-interaction)  Do not ask any interactive question

AVAILABLE COMMANDS
  about                  Shows information about Poetry.
  add                    Adds a new dependency to pyproject.toml.
  build                  Builds a package, as a tarball and a wheel by default.
  cache                  Interact with Poetry's cache
  check                  Checks the validity of the pyproject.toml file.
  config                 Manages configuration settings.
  debug                  Debug various elements of Poetry.
  env                    Interact with Poetry's project environments.
  export                 Exports the lock file to alternative formats.
  help                   Display the manual of a command
  init                   Creates a basic pyproject.toml file in the current directory.
  install                Installs the project dependencies.
  lock                   Locks the project dependencies.
  new                    Creates a new Python project at <path>.
  publish                Publishes a package to a remote repository.
  remove                 Removes a package from the project dependencies.
  run                    Runs a command in the appropriate environment.
  search                 Searches for packages on remote repositories.
  self                   Interact with Poetry directly.
  shell                  Spawns a shell within the virtual environment.
  show                   Shows information about packages.
  update                 Update the dependencies as according to the pyproject.toml file.
  version                Shows the version of the project or bumps it when a valid bump rule is provided.

userid@DESKTOP-YourID:~$ python
Python 3.8.2 (default, Apr  4 2020, 23:48:47)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

I put nodejs in addition to python on my PC, but it is clean on brew (hugo is introduced separately for the purpose of blogging). I think this is a good point of this method.

$ brew list
anyenv  hugo  patchelf

How to study package management tool poetry

The official document has been translated into Japanese (below), so it seems good to start here. https://cocoatomo.github.io/poetry-ja/basic-usage/

Let's also use winpython (portable version is recommended) as appropriate.

It is important to manage the version of pyhon and packages by the procedure described here, such as when using python for web development with tight delivery time, if there is even one package that does not work. For data engineer / data science projects, you need to connect to a rather designated (often legacy) database. When connecting to an older database, the linux version of the driver may not be possible. If pyenv is completed on wsl, coexistence with winpython is of course no problem. Let's use winpython as appropriate to secure the option of database connection via ODBC etc. With the portable version, there is no worry that the environment will get dirty, so with python alone, winpython, which also introduces anaconda and pandas / numpy without permission, may be the strongest. However, when it comes to linking with other tools via the shell, I still want a wsl pyenv.

[Bonus] Nodejs version control is the same.

If you follow this procedure, you can easily manage the version of nodejs. If you want to install the latest version of nodejs at the moment, follow the steps below.

anyenv install nodenv
exec $SHELL -l
touch $(nodenv root)/default-packages
nodenv install 13.2.0
nodenv global 13.12.0

You can check the version with node -v.

Recommended Posts

How to get started with the 2020 Python project (windows wsl and mac standardization)
How to get started with Python
How to get the date and time difference in seconds with python
Link to get started with python
How to get the Python version
How to get started with Scrapy
How to get started with Django
Minimum knowledge to get started with the Python logging module
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
Get started with the Python framework Django on Mac OS X
How to get followers and followers from python using the Mastodon API
[Python] How to get the first and last days of the month
How Python beginners get started with Python with Progete
How to get started with laravel (Linux)
[Python Kivy] How to get the file path by dragging and dropping
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
The easiest way to get started with Django
How to get the files in the [Python] folder
A layman wants to get started with Python
For those who want to learn Excel VBA and get started with Python
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the variable name itself in python
How to get the number of digits in Python
[Introduction to Python] How to iterate with the range function?
I tried to get started with blender python script_Part 01
[Python] How to specify the download location with youtube-dl
How to share folders with Docker and Windows with tensorflow
I tried to get started with blender python script_Part 02
How to get mouse wheel verdict with Python curses
[Python] How to rewrite the table style with python-pptx [python-pptx]
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
How to install Python [Windows]
How to get started with Visual Studio Online ~ The end of the environment construction era ~
[Note] How to write QR code and description in the same image with python
Get started with Python! ~ ② Grammar ~
How to set the development environment for each project with VSCode + Python extension + Miniconda
I tried to simulate how the infection spreads with Python
Let's get started with Python ~ Building an environment on Windows 10 ~
[Python] How to play with class variables with decorator and metaclass
How to do Bulk Update with PyMySQL and notes [Python]
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
Get additional data to LDAP with python (Writer and Reader)
How to log in to AtCoder with Python and submit automatically
Get the source of the page to load infinitely with python.
Use Python to monitor Windows and Mac and collect information on the apps you are working on
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
Python: How to use async with
Get started with Python! ~ ① Environment construction ~
Python 3.6 on Windows ... and to Xamarin.
Get the weather with Python requests
Get the weather with Python requests 2
[Python] How to import the library