――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