LoginSignup
0
0

More than 1 year has passed since last update.

Pythonの始め方

Last updated at Posted at 2021-10-11

Summary

最初に入れるやつらに付け足すpythonのパートが長いので分離。

MacではZshを、WindowsではPowerShell。
Python管理は、Macではpyenv、Windowsではpyenv-win
パッケージ管理

  • 基本的にpip+pip-review
  • コマンド系は、pipx
  • プロジェクトは、poetry

Macではエスケープキーが\だが、Windowsでは`
ここでは\に統一するので、Windowsの場合は適宜置換する。

以前は、Anaconda/Minicondaを使っていたが、
M1 MacになってMKLの恩恵も受けれないし、
Anacondaの商業利用の制限も怖いしでPyPiに戻ってきた。

python

pyenvpipxgiboはHomebrewとかで入れとく。

pyenv install 3.x.x
pipx install \
    poetry \
    pip-search
poetry config virtualenvs.in-project true

Package

pip install \
    astropy \
    autopep8 \
    black \
    conda-build \
    docopt \
    dropbox \
    ipympl \
    ipython \
    isort \
    jupyter \
    jupyterlab \
    jupyterlab_code_formatter \
    matplotlib \
    nidaqmx-python \
    nodejs \
    numpy \
    pandas \
    pdf2image \
    pre-commit \
    pycodestyle \
    pydocstyle \
    pyperclip \
    pyvisa-py \
    scipy \
    selenium \
    timeout-decorator \
    tqdm \
    mpl_interactions \
    nbmerge

Jupyter Lab

jupyter labextension install \
    @jupyter-widgets/jupyterlab-manager \
    @ryantam626/jupyterlab_code_formatter \
    jupyter-matplotlib
jupyter serverextension enable --py jupyterlab_code_formatter

Jupyter Notebook with Mathematica

Launcher for JupyterLab

JupyterLabは基本的にいつも起動しておく: y-marui/keep-alive-jupyter

ghq get https://github.com/y-marui/keep-alive-jupyter.git
g keep-alive-jupyter
ln -s $(pwd)/com.bill.jupyter.plist ~/Library/LaunchAgents/com.bill.jupyter.plist
launchctl load ~/Library/LaunchAgents/com.bill.jupyter.plist

CUI

nbopenが良さそう。

GUI

Make Jupyter Launcher.app with Automater. Run zsh script with pass input as arguments.

nbopen_path=`which nbope`
if [[ -s "$jupyter_launcher_path" ]]; then
	$nbopen_path "$@"
fi

Update

Add update to pyenv

git clone https://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update

Command to update

pyenv update
pip-review --auto
jupyter labextension update --all

Package開発

自作Packageは、使いまわすこともあるので、雑でも良いからGitHubとかにあげとく。
パッケージ名をPACKAGEとして進める。

ghq get PACKAGE
g PACKAGE

poetry

poetry init
poetry install
poetry shell

次からは、

poetry shell

だけでで良い。
JupyterLabのカーネルとして使う場合は、個別に追加する必要がある。
名前package-nameと表示名Package Nameを分けることができる。

epo_name="package-name"
disp_name="Package Name"
ipython kernel install --user --name="${repo_name}" --display-name="${disp_name}" --env PYTHONPATH "\${PYTHONPATH}:$(pwd)"

JupyterのKernelを確認。

jupyter kernelspec list

プロジェクトがひと段落したらJupyterLabのカーネルから削除。

jupyter kernelspec uninstall "${repo_name}"

新しいパッケージを作るときは、ipykernelpytestをdevelop用に入れとくといい。

poetry add -D ipykernel pytest

普通の依存はpoetry add、develop用はpoetry add -Dで追加する。

References

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0