A modern environment building procedure for those who want to get started with Python right away

Introduction

――There are various options for building a Python environment, and I don't know what it is. ――Installing Anaconda is fast, but there are various disadvantages, and you may want to manage versions and packages for each project without using Anaconda. ――Especially in the case of Python, there are many opportunities to start without building an environment such as Google Colaboratory or external learning materials, so when you try to build an environment yourself, you may get "... what? What should I do?" (I did) --For example, for package management, the method of creating and managing requirements.txt, a file that lists the package name to be used and its version, was used, but now it is regarded as a legacy method. ing ――We want to build an environment that takes into account such situations and the latest trends as much as possible. ――Specifically, I use a package manager called poetry, which is said to be hot recently.

Premise of this article

--Target is ** Mac **. --Install Python version manager and package manager, and build an environment where you can start development using Python. --In this article, ** Anaconda is not used and the environment is built **. --The items to be handled (the items to be installed in this article) are as follows. - Homebrew - anyenv - pyenv - poetry

environment

1. Install Homebrew

-Homebrew official website says "Paste this script into the terminal and execute it." Copy it and paste it into the terminal and execute it. スクリーンショット 2020-03-02 19.05.59.png

2. Install anyenv

What is anyenv (link to GitHub)

--Summary of things named "○○ env" (pyenv, nodenv handled by JavaScript (or Node.js), etc., mainly used to easily switch language versions for each project) A convenient one to manage. ――It's also convenient when you later try to develop in another language.

anyenv installation procedure

  1. Use Homebrew installed above. In the terminal (zsh in my case) do the following:

    $brew install anyenv
    
  2. Next, execute the following in the terminal.

    $anyenv init
    
  3. Then, an instruction will appear, and according to it, add ʻeval "$ (anyenv init-)" `to ~ / .zshrc (~ / .bashrc in the case of bash). Specifically, it is as follows.

    vi ~/.zshrc
    
    eval "$(anyenv init -)"
    
  4. Reboot the terminal

  5. Warning will appear when the terminal is restarted, so execute the following as instructed.

    $anyenv install --init
    

Install pyenv

What is pyenv (link to GitHub)

--Used for Python version control. --You can easily switch between Python versions for each project.

pyenv installation procedure

  1. Install pyenv from anyenv installed above.

    $ anyenv install pyenv
    
  2. Execute the following in the terminal (command to start the running shell ($ SHELL) from the login shell)

    $ exec $SHELL -l
    
  3. Next, execute the following in the terminal to get a list of Python versions that can be installed.

    $pyenv install -l
    
  4. After that, specify the version you want to install (3.8.2 in the example below) and execute the following in the terminal. --You can check the latest version etc. from Python official site.

    $pyenv install 3.8.2
    
  5. Then, execute the following in the terminal.

    $pyenv rehash
    
  6. Finally, you can use the installed version of Python in any project by running the following command in the terminal.

    $pyenv global 3.8.2
    

installation of poetry

What is poetry?

--Python dependency and package manager development started in 2018 --It can be handled like npm or yarn in JavaScript

Install poetry by running the following in the terminal

-Install as described in Official GitHub

Terminal


curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

Through Path

Terminal


$ echo '$HOME/.poetry/bin:$PATH' >> ~/.zshrc (for bash.bashrc)
$ source ~/.zshrc (for bash.bashrc)

By the way

--In addition, by executing the following, a virtual environment will be created directly under each project, so do it for the time being -(By default, a virtual environment is created in ~ / Library / Caches / pypoetry / virtualenvs)

Terminal


$ poetry config settings.virtualenvs.in-project true

Make a project

――The environment construction is completed for the time being, so let's create a project.

Terminal


$ mkdir my-project (directory name can be anything)

--Move to the created directory-> Specify the Python version used in the project-> Execute poetry init --At this time (when specifying the version), note that the version must be installed in advance, such as pyenv install 3.8.2.

Terminal


$ cd my-project
$ pyenv local 3.8.2
$ poetry init

--When you do poetry init, questions are displayed interactively, and when you answer those questions, the project settings are completed and a configuration file called pyproject.toml is created. --When the settings are complete, do poetry install.

Terminal


$ poetry install

--This will install the specified package (if you specified the required package in the interactive question above). ――This is the end for the time being.

How to run Python

--Create a Python file in the created directory (project).

main.py


print('hello python world!')

--In the directory, do the following

Terminal


$ poetry run python main.py

--This will run the Python file.

Terminal (output)


hello python world!

How to use Jupyter Notebook

--In the project (directory), install jupyter by the following

Terminal


$ poetry install jupyter

--Jupyter Notebook will be launched by executing the following

Terminal


$ poetry run jupyter notebook

Reference article

  1. From Python package development using Poetry to PyPI release
  2. https://qiita.com/yano404/items/85f21897e417f03236c9
  3. Poetry installation and initial setup

Recommended Posts

A modern environment building procedure for those who want to get started with Python right away
For those who want to learn Excel VBA and get started with Python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
For those who want to write Python with vim
Environment construction for those who want to study python easily with VS Code (for Mac)
Building a Windows 7 environment for getting started with machine learning with Python
Python techniques for those who want to get rid of beginners
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
A layman wants to get started with Python
Procedure for building a CDK environment on Windows (Python)
The first step of machine learning ~ For those who want to implement with python ~
❤️ Bloggers ❤️ "Beloved BI" ❤️ Let's get started ❤️ (For those who can make charts with Python)
Let's get started with Python ~ Building an environment on Windows 10 ~
Building a Hy environment for Lisper who hasn't touched Python
For those who want to start machine learning with TensorFlow2
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Building a virtual environment with Python 3
How to get started with Python
Environment construction procedure for those who are not familiar with the python version control system
PyPI registration steps for those who want to make a PyPI debut
[Python] A memo that I tried to get started with asyncio
Python environment construction 2016 for those who aim to be data scientists
From building a Python environment for inexperienced people to Hello world
A memo for those who want quick socket communication with netcat
Building a Python3 environment with Amazon Linux2
Building a Python 3.6 environment with Windows + PowerShell
I want to build a Python environment
[Mac] Building a virtual environment for Python
Build a modern Python environment with Neovim
Building a Python development environment for AI development
Building a virtual environment for Mayavi dedicated to Python 3.6, Anaconda, Spyder users
Procedure for building a kube environment on amazon linux2 (aws) ~ (with bonus)
Let's get started with Python 3. ~ For kittens who are wondering what to choose as their main language ~
Get a ticket for a theme park with python
Building a python environment with virtualenv and direnv
Building an Anaconda environment for Python with pyenv
Building a Python environment with WLS2 + Anaconda + PyCharm
I want to make a game with Python
Procedure for creating a LineBot made with Python
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
I want to write to a file with Python
Get a quick Python development environment with Poetry
I want to create a nice Python development environment for my new Mac
Notes from installing Homebrew to building an Anaconda environment for Python with pyenv
Join Azure Using Go ~ For those who want to start and know Azure with Go ~
[Solved] I have a question for those who are familiar with Python mechanize.
[Introduction to Python] How to get the index of data with a for statement
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Write about building a Python environment for writing Qiita Qiita
[Python] I want to use only index when looping a list with a for statement
Recommendation of building a portable Python environment with conda
Building a Docker working environment for R and Python
I tried to get started with blender python script_Part 01
Build a python environment for each directory with pyenv-virtualenv
I want to tell people who want to import from a higher directory with Python direnv
I tried to get started with blender python script_Part 02
Building an environment for natural language processing with Python
I want to work with a robot in python.