Python実行・開発環境

in  

Pythonの実行・開発環境について

pip

モジュールをターゲットを指定してインストール

1
$ pip install requests -t functions/python_lambda

requirements.txtからインストール

1
$ pip install --requirement functions/myfunc/requirements.txt -t functions/myfunc/

pyenv

Mac

1
$ brew install pyenv

~/.bash_profile

1
2
3
4
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PATH=$HOME/.nodebrew/current/bin:$PATH

3.6.1をインストール

1
2
3
4
5
$ pyenv install 3.6.1
$ pyenv rehash
$ pyenv global 3.6.1
$ python -V
Python 3.6.1
pyenv install –listインストール可能なバージョン一覧を表示する

Amazon Linux https://qiita.com/rysk92/items/878fddbf23262628d89e

pipenv

1
2
3
4
$ pip install pipenv
$ cd src/python/
$ pipenv --three
$ pipenv install

環境に入ってモジュールをインストールする。

1
2
$ pipenv shell
$ pipenv install twilio

Pipfileは下記のようになります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
twilio = "*"

[requires]
python_version = "3.7"

仮想環境の情報を表示します。

1
2
$ pipenv --venv
/Users/hoge/.local/share/virtualenvs/monitor-ywBsqNFq

脆弱性チェック

1
2
3
4
5
$ pipenv check
Checking PEP 508 requirements…
Passed!
Checking installed package safety…
All good!

Trouble Shooting

  1. pip shellで “Reason: image not found” と出る場合。

HomeBrewとの連携の兼ね合いでこうなる場合があるので、一度仮想環境を消して再作成する。

1
2
rm -rf `pipenv --venv`
pipenv install --dev

開発環境

EmacsでPython開発環境を構築する方法です。 pipenv環境を前提としています。

開発環境にpython language serverをインストールします。

1
$ pipenv install --dev python-language-server[all]

Caskファイルを編集します。

1
2
3
4
5
6
(depends-on "pipenv")
(depends-on "lsp-mode")
(depends-on "lsp-treemacs")
(depends-on "lsp-ui")
(depends-on "company-lsp")
(depends-on "helm-lsp")

EmacsのPackageをインストール

1
$ cask install

.init.elを編集します。 python-modeの起動とともにlspが起動します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(use-package pipenv
  :hook (python-mode . pipenv-mode)
  :init
  (setq
   pipenv-projectile-after-switch-function
   #'pipenv-projectile-after-switch-extended))

(use-package lsp-mode
  :ensure t
  :commands lsp
  :custom
  (lsp-auto-guess-root nil)
  :bind (:map lsp-mode-map ("C-c C-f" . lsp-format-buffer))
  :hook (python-mode . lsp)
  )


(use-package lsp-ui
  :after lsp-mode
  :diminish
  :commands lsp-ui-mode
  :custom-face
  (lsp-ui-doc-background ((t (:background nil))))
  (lsp-ui-doc-header ((t (:inherit (font-lock-string-face italic)))))
  :bind (:map lsp-ui-mode-map
	      ([remap xref-find-definitions] . lsp-ui-peek-find-definitions)
	      ([remap xref-find-references] . lsp-ui-peek-find-references)
	      ("C-c u" . lsp-ui-imenu))
  :custom
  (lsp-ui-doc-enable t)
  (lsp-ui-doc-header t)
  (lsp-ui-doc-include-signature t)
  (lsp-ui-doc-position 'top)
  (lsp-ui-doc-border (face-foreground 'default))
  (lsp-ui-sideline-enable nil)
  (lsp-ui-sideline-ignore-duplicate t)
  (lsp-ui-sideline-show-code-actions nil)
  :config
  (setq lsp-ui-doc-use-webkit t)
  (defadvice lsp-ui-imenu (after hide-lsp-ui-imenu-mode-line activate)
    (setq mode-line-format nil)))

(use-package company-lsp :commands company-lsp)
(use-package helm-lsp :commands helm-lsp-workspace-symbol)

M-x pipenv-activateを実行します。C-x C-pで立ち上がるPython shellがpipenv環境のpythonで立ち上がります。 また、コードジャンプが可能になります。


Share