What's this? Python environment for macbook
Since it was built in, it is a memo. The environment is ** mac OS 10.14.6 Mojave **. We will proceed on the assumption that homebrew is already included. (If you do not have homebrew, please refer to Homebrew Official and add it) I think I will update it from time to time.
Install system python3 with homebrew. If you already have it, please skip it. Also, please skip people who say, "I'll put pyenv later. I'll put it in pyenv." Installing python3 on the system is my hobby. (~~ Also, I don't know what it is, but when I try to link numpy and scipy with mkl with python on pyenv, it fails ... ~~ For more information here)
$ brew install python
Enter pyenv
according to Official.
I think brew install pyenv
is easy, but I don't really like PYENV_ROOT = / usr / local / var / pyenv
(it's a complete hobby. I also want to make it as similar as possible to a Linux environment. ), So enter it with git
(brew install pyenv
is no problem at all).
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
After installing with, edit .bash_profile
to run & pyenv init
through PATH
.
$ 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
Restart the shell and you will be able to use pyenv
.
If you don't have python3 installed on your system, do pyenv install 3.7.6
, pyenv global 3.7.6
here.
If pyenv install
fails, installing the latest commandlinetools from https://developer.apple.com/download/more/?=command%20line%20tools may work.
Follow Official.
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3
Also my hobby is to make .venv
in the project directory
$ poetry config virtualenvs.in-project true
To run. The settings around here
$ poetry config --list
You can see it at.
If you roughly explain to people "What is pipx in the first place?" ...
There is an application (command) to be installed with pip (such as sphinx-quickstart
).
I use that kind of thing in almost any virtual environment.
Burns It's a hassle to install in all virtual environments.
Therefore, the one that makes it possible to use such applications (commands) that can be entered with pip globally.
is. The official is here.
Ignore those people and move on.
For those who installed python with homebrew
$ brew install pipx
$ pipx ensurepath
For those who make python global in pyenv
$ python3 -m pip install --user pipx
$ python3 -m pipx ensurepath
Add the following to .bash_profile
to enable tab completion.
eval "$(register-python-argcomplete pipx)"
This is the end. Thank you for your hard work.
There are two ways to create an environment with poetry, poetry new
and poetry init
.
poetry new my-package
--Create a directory and create a package template under it.
--The directory structure looks like thismy-package
├── pyproject.toml
├── README.rst
├── my_package
│ └── __init__.py
└── tests
├── __init__.py
└── test_my_package.py
poetry init
--Answer the question and create pyproject.toml
in the directory where you ran the command.If you want to create a package, you should recognize it as poetry new <my-package>
, otherwise you should recognize it as poetry init
.
Probably the latter is often the case, so here we will use poetry init
to create the environment.
First, install python used in the project with pyenv.
Let's use 3.7.4
here.
$ pyenv install 3.7.4
Create a directory for your project.
$ mkdir /foo/bar/project
After specifying the version of python to use with pyenv local
, poetry init
.
$ cd /foo/bar/project
$ pyenv local 3.7.4
$ poetry init
(If you answer what you are asked, pyproject.toml is generated)
(Ignore the question and later pyproject.You may edit toml)
(First of all, pyproject by yourself.You may make toml)
You can install the package as written in pyproject.toml
with poetry install
.
$ poetry install
(This is pyproject.The package written in toml is installed)
(at this point`.venv`Is also made)
You can also add a package with poetry add <package>
.
For more information on how to use poetry, see Official documentation.
The virtual environment (.venv
) is created automatically when you first use poetry add
or poetry install
, but before that, create a virtual environment such as python -m venv .venv
. Is also good.
You might think that's necessary, but it seems like this is the only way to do it when you need pip.conf
(I don't know of any other way so far).
pip.conf
should be under .venv
(it will be .venv / pip.conf
).
By the way, in my case, pip.conf ($ HOME / .pip / pip.conf
) of system python3
[install]
no-binary = numpy,scipy
[wheel]
no-binary = numpy,scipy
If you do nothing, you will be affected by this.
So, when using numpy, scipy
, I purposely create .venv / pip.conf
with the following contents.
[install]
no-binary =
[wheel]
no-binary =
If you do not devise in relation to mkl, the build of numpy, scipy
will fail, so I will do this (~~ I will write about this later ~~ I made an article → numpy, scipy under pyenv + poetry environment to mkl I want to use it).
For pipx
, I think it's better to see the usage example, so I will introduce the procedure to install sphinx
using pipx
.
As mentioned above, it is troublesome (or never done) to put sphinx
in all virtual environments, so pipx
comes into play.
$ pipx install sphinx
If you want to use sphinx_rtd_theme
or sphinx contrib-blockdiag
,
$ pipx inject sphinx sphinx_rtd_theme
$ pipx inject sphinx sphinxcontrib-blockdiag
You can do it.
I used to use pipenv
, but poetry
should install the package faster than pipenv
.
However, I don't make many packages, so when asked if it needs to be poetry
, it's not so.
If there is something else that looks good, I may switch again (I've been repeating this all the time ... Is there a goal?)
Recommended Posts