Python explosive environment construction starting from zero (Mac)

――There are many variations in building a Python environment and it is difficult to understand, but I will present the method that I feel is the best at the moment. --Specifically, pyenv + Poetry

Pyenv

brew install pyenv

Install with.

First, set the environment variables. Register PYENV_ROOT as the path $ HOME / .pyenv, include $ PYENV_ROOT / bin in PATH, and have pyenv init at shell startup.

For example, in bash

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
exec "$SHELL"

It will be. For other shells, please check Documentation.

Now that you're ready, put in your favorite version of Python.

pyenv install 3.7.2
pyenv global 3.7.2

You are now ready to use Python v3.7.2. Type python --version and make sure you get Python 3.7.2.

Poetry

Poetry is an up-and-coming package management tool. A similar product is Pipenv, but it's much faster than Pipenv, and Pipenv is currently stuck updating, so if you're new to Python, you should use Poetry.

Installation is

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

It will be completed with.

There is only one setting to do. that is

poetry config virtualenvs.in-project true

is. Now, the .venv directory will be created locally (in the same hierarchy as pyproject.toml), so when you open the directory with VSCode or IDE, it will automatically recognize the virtual environment. Will be.

Let's try it out. Prepare a sample directory.

mkdir example
cd example

When you do poetry init, you will interactively decide what to write in pyproject.toml. If you are a Node.js user, you should be familiar with npm init etc.

What's interesting is that if you have already decided which dependencies you want to include at this point, you can enter them in sequence by answering yes withWould you like to define your main dependencies interactively?. is. Of course, you can add dependent packages later, so don't worry.

Pyproject.toml is written out when the conversation is over.

Now let's write the program. I will scrape Hiroshi Abe's homepage with the explosive connection. First, put in the familiar requests for Python scraping.

poetry add requests

You should now have a new .venv directory in your directory as soon as you get the requests. By the way, if you open the directory with VSCode in this state, a display like Python 3.7.2 64-bit ('.venv': venv) will appear at the bottom left, and you can see that it recognizes the local virtual environment. I will.

Then write a scraping program.

abe.py


import requests

r = requests.get("http://abehiroshi.la.coocan.jp/")
print(r.text)

Let's move it. VSCode is smart enough that the shell will automatically enter the virtual environment when you open a terminal within VSCode. So normally

python abe.py

Just OK.

If you want to type commands in the console instead of in VSCode

poetry run python abe.py

Or

poetry shell
python abe.py

It will be.

Recommended Posts

Python explosive environment construction starting from zero (Mac)
Mac environment construction Python
Python environment construction For Mac
I checked Mac Python environment construction
Python environment construction memo on Mac
Environment construction of python3.8 on mac
Python3 TensorFlow for Mac environment construction
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Collecting information from Twitter with Python (Environment construction)
python windows environment construction
homebrew python environment construction
Anaconda-4.2.0-python3 environment construction (Mac)
Python development environment construction
python2.7 development environment construction
From Python environment construction to virtual environment construction with anaconda
Pyxel environment construction (Mac)
Python environment construction @ Win7
Python development environment construction 2020 [From Python installation to poetry introduction]
Environment construction with anyenv + pyenv (migrate from pyenv only (Mac))
Procedure to exe python file from Ubunts environment construction
Python + Anaconda + Pycharm environment construction
ChIP-seq analysis starting from zero
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Anaconda3 python environment construction procedure
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Environment construction of python2 & 3 (OSX)
Ansible environment construction For Mac
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
[Tweepy] Re: Twitter Bot development life starting from zero # 1 [python]
[Python] Anaconda environment construction (installation, startup, virtual environment, package management) Mac environment
VScode environment construction (on Mac) & graph display in Python (@browser)
Building a Python environment on Mac
Environment construction of python and opencv
Mac + Eclipse (PyDev) + Django environment construction
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Anaconda python environment construction on Windows 10
Error when building mac python environment
Python + Unity Reinforcement learning environment construction
Anaconda environment construction on Mac (2018 version)
Create a Python environment on Mac (2017/4)
Python environment construction (pyenv, anaconda, tensorflow)
[Python3] Development environment construction << Windows edition >>
Code wars kata starting from zero
Python development environment construction on macOS
Python environment construction (pyenv + poetry + pipx)
View Mac desktop notifications from Python
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
From Ubuntu 20.04 introduction to environment construction
Emacs Python development environment construction memo