pipenvの記事ではありません。
2020年にもなってなぜcondaなのか。それは困っていなから。遅延評価。
Visual Studio Code にPython環境構築
主に重要なのは以下の3つ
- autopep8によるコーディング規約
- flake8によるコードフォーマット
- condaによる実行環境構築(仮想環境へのpath)
condaに パッケージインポート
conda環境構築の話は割愛
TLTR
これでよい。
conda install -c anaconda flake8 autopep8
conda installでよくあるエラー
~ conda install flask8 autopep8 (py_38)
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- flask8
Current channels:
- https://repo.anaconda.com/pkgs/main/osx-64
- https://repo.anaconda.com/pkgs/main/noarch
- https://repo.anaconda.com/pkgs/r/osx-64
- https://repo.anaconda.com/pkgs/r/noarch
- https://conda.anaconda.org/conda-forge/osx-64
- https://conda.anaconda.org/conda-forge/noarch
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
原因
condaパッケージインストーラーが参照する先に目的のパッケージがないから。
よって、参照先を増やせば良い。
解決策1
参照先は以下のコマンドで参照できる。
conda config --get channels
これに対して、anaconda
conda-forge
を追加する。
conda config --append channels anaconda
conda config --append channels conda-forge
これで改めてconda install [パッケージ名]
すればよい。
解決策2
環境はいじらず、一時的に参照先を指定できれば良いという方には、オプションで参照先を指定すれば良い。
conda install -c [参照先] [パッケージ名]
conda install -c anaconda flake8 autopep8
VScodeの設定
コマンドパレットにopen settings(JSON)
で環境設定からjsonファイルを開く。
各言語の設定を書くことができる。(らしい)
ここに、tabの設定、保存するときに自動整形する、一行の制限文字数の拡大などを書く。
flack8,autopep8を導入したので、ここに書いておく。
}
中略
"[python]": {
"editor.tabSize": 4,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
},
"python.linting.lintOnSave": true,
"python.linting.pylintEnabled": false,
"python.linting.pycodestyleEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [
"--ignore=W293, W504",
"--max-line-length=150",
"--max-complexity=20"
],
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
"--aggressive",
"--aggressive",
],
}
VScodeからconda仮想環境へpathをつなぐ
conda仮想環境にインストールしたため、先程のファイルは実行したままではVScodeがデフォルトのpythonを参照するためにうまく動きません。具体的にはautopep8がないよ〜となります。
つなぎたいcondaのpathを確認
ここは自分の環境に合わせてください。
~ conda info --envs (py_38)
# conda environments:
#
base /Users/hoge/.pyenv/versions/anaconda3-5.3.0
py_38 * /Users/hoge/.pyenv/versions/anaconda3-5.3.0/envs/py_38
pathをVScodeに設定する
Control + command + p
→open settings(UI)
を開き、設定の検索にpython.pythonpath
と入力します。
するとデフォルトではpython
とだけあります。これを上記のpathに変更
/Users/hoge/.pyenv/versions/anaconda3-5.3.0/envs/py_38
これでよい。はず。
VScodeに感心したこと。
再起動することなく、設定ファイルが読み込まれ、すでに開かれているファイルに対して自動整形とpep8ができたこと。
参考文献
https://qiita.com/irukiti/items/5d523c338f4affc9f722
https://qiita.com/propella/items/247beb6be15bdef66ecc#conda
https://anaconda.org/anaconda/flake8