――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.
--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
-Homebrew official website says "Paste this script into the terminal and execute it." Copy it and paste it into the terminal and execute it.
--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.
Use Homebrew installed above. In the terminal (zsh in my case) do the following:
$brew install anyenv
Next, execute the following in the terminal.
$anyenv init
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 -)"
Reboot the terminal
Warning will appear when the terminal is restarted, so execute the following as instructed.
$anyenv install --init
--Used for Python version control. --You can easily switch between Python versions for each project.
Install pyenv from anyenv installed above.
$ anyenv install pyenv
Execute the following in the terminal (command to start the running shell ($ SHELL
) from the login shell)
$ exec $SHELL -l
Next, execute the following in the terminal to get a list of Python versions that can be installed.
$pyenv install -l
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
Then, execute the following in the terminal.
$pyenv rehash
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
$ pyenv install (version number)
, then in that directory $ pyenv local (version number) You can switch the version for each project by typing
.--Python dependency and package manager development started in 2018 --It can be handled like npm or yarn in JavaScript
-Install as described in Official GitHub
Terminal
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
Terminal
$ echo '$HOME/.poetry/bin:$PATH' >> ~/.zshrc (for bash.bashrc)
$ source ~/.zshrc (for bash.bashrc)
--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
――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.
--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!
--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
Recommended Posts