{"id":361,"date":"2019-08-05T22:26:37","date_gmt":"2019-08-05T13:26:37","guid":{"rendered":"http:\/\/localhost:8000\/?p=361"},"modified":"2021-01-18T02:22:57","modified_gmt":"2021-01-17T17:22:57","slug":"python","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/08\/python.html","title":{"rendered":"python\u306eTips"},"content":{"rendered":"

\u4ed5\u4e8b\u3067python\u3092\u89e6\u308b\u6a5f\u4f1a\u304c\u4eca\u5f8c\u5897\u3048\u305d\u3046\u306a\u306e\u3067\u3001python\u306b\u95a2\u3059\u308b\u30e1\u30e2\u3092\u305f\u3081\u3066\u3044\u304f\u4e88\u5b9a\u3067\u3059\u3002<\/p>\n

Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\uff08pyenv\uff09<\/h2>\n

pyenv\u3092\u4f7f\u3063\u3066python\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059<\/p>\n

# pyenv\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\nbrew install pyenv\npyenv -v\n> pyenv 1.2.13\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u53ef\u80fd\u306apython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\npyenv install --list\n\n# \u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306epython\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\npyenv install  3.7.4\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u306epython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\npyenv versions\n> * system (set by \/Users\/rinoguchi\/.pyenv\/version)\n>   3.7.4\n\n# ~\/.bash_profile\u306b\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\uff08syms\u3068autocompletion\u306e\u6709\u52b9\u5316\uff09\necho 'export PYENV_ROOT="$HOME\/.pyenv"' >> ~\/.bash_profile\necho 'export PATH="$PYENV_ROOT\/bin:$PATH"' >> ~\/.bash_profile\necho -e 'if command -v pyenv 1>\/dev\/null 2>&1; then\\n  eval "$(pyenv init -)"\\nfi' >> ~\/.bash_profile\nsource ~\/.bash_profile\n\n# python\u30b3\u30de\u30f3\u30c9\u306e\u53c2\u7167\u5148\n# - python\u30b3\u30de\u30f3\u30c9\u3082pip\u30b3\u30de\u30f3\u30c9\u3082.pyenv\/shims\u914d\u4e0b\u3092\u898b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u308a\n# - \u5b9f\u4f53\u306fbash\u3067\u3001\u6700\u7d42\u7684\u306b\u306f\u6307\u5b9a\u3055\u308c\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u306e.pyenv\/versions\u914d\u4e0b\u3092\u898b\u306b\u3044\u3063\u3066\u304f\u308c\u308b\u307d\u3044\nwhich python\n> \/Users\/rinoguchi\/.pyenv\/shims\/python\nwhich pip\n> \/Users\/rinoguchi\/.pyenv\/shims\/pip\n\n# \u30b0\u30ed\u30fc\u30d0\u30eb\u3067\u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u6307\u5b9a\npyenv global 3.7.4 # .pyenv\/version\u306b\u30d0\u30fc\u30b8\u30e7\u30f3\u756a\u53f7\u304c\u8a18\u8f09\u3055\u308c\u308b\npyenv versions\n>   system (set by \/Users\/rinoguchi\/.pyenv\/version)\n> * 3.7.4\n\n# \u7279\u5b9a\u306e\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u3060\u3051\u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u6307\u5b9a\n# - \u5b9f\u884c\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u76f4\u4e0b\u306b.pyenv-version\u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\u3055\u308c\u308b\n# - \u3053\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u3053\u306e\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u3067\u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u56fa\u5b9a\u3055\u308c\u308b\n# - git\u30ea\u30dd\u30b8\u30c8\u30ea\u306bcommit\u3059\u308b\u306e\u3082\u3042\u308a\ncd hoge\/fuga\npyenv local 2.7.3\n\n# pyenv local\u3092\u89e3\u9664\npyenv local --unset\n# .pyenv-version\u30d5\u30a1\u30a4\u30eb\u524a\u9664\u3055\u308c\u308b<\/code><\/pre>\n

Python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u304a\u3088\u3073\u4eee\u60f3\u74b0\u5883\u7ba1\u7406\uff08poetry\uff09<\/h2>\n

\u3053\u308c\u307e\u3067pip + virtualenv\uff08venv\uff09\u3092\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001poetry\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002\u8a73\u7d30\u306f\u5225\u8a18\u4e8b\u306b\u3057\u305f\u306e\u3067\u305d\u3061\u3089\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002
\npoetry\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u30fb\u4eee\u60f3\u74b0\u5883\u3092\u7ba1\u7406<\/a><\/p>\n

Python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\uff08pip\uff09<\/h2>\n

python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0pip\u3092\u5229\u7528\u3059\u308b\u3002\u3053\u308c\u306fpython 3.4\u4ee5\u964d\u306fpython\u672c\u4f53\u3068\u4e00\u7dd2\u306binstall\u3055\u308c\u307e\u3059\u3002
\n\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5148\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3060\u3068python\u672c\u4f53\u914d\u4e0b\u306e\/lib\/pythonX.Y\/site-packages<\/code>\u30d5\u30a9\u30eb\u30c0\u306a\u306e\u3067\u3001\u540c\u4e00\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u3046\u5834\u5408\u306b\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u5171\u6709\u3055\u308c\u307e\u3059\u3002<\/p>\n

# \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\npip install numpy\n\n# \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u524a\u9664\u3059\u308b\npip uninstall numpy\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u78ba\u8a8d\u3059\u308b\npip list\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5bfe\u8c61\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\uff08requirements.txt\uff09\u3067\u7ba1\u7406\u3059\u308b\nvi requirements.txt\n\n# \u73fe\u5728\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u5f62\u5f0f\u3067\u8868\u793a\u3059\u308b\uff08\u3053\u308c\u3092requirements.txt\u306b\u30b3\u30d4\u30fc\u3059\u308b\uff09\npip freeze\n> pylint==2.3.1\n> pyspark==2.4.3\n> numpy==1.17.0\n\n# \u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u3057\u305f\u304c\u3063\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\npip install -r requirements.txt\n\n# \u7279\u5b9a\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\n# - -t\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u53ef\u80fd\u3060\u304c\u3001import\u3059\u308b\u969b\u306b\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u691c\u7d22\u30d1\u30b9\u3092\u89e3\u6c7a\u304c\u5fc5\u8981\u306b\u306a\u308b\u306e\u3067\u304a\u3059\u3059\u3081\u3057\u306a\u3044\u3002\npip install -t my_packages numpy<\/code><\/pre>\n

Python\u306e\u4eee\u60f3\u74b0\u5883\u69cb\u7bc9\uff08virtualenv\uff09<\/h2>\n

virtualenv\u3067\u9694\u96e2\u3055\u308c\u305fPython\u306e\u4eee\u60f3\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002
\n\u5177\u4f53\u7684\u306b\u306f\u3001python\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5168\u4f53\u304c\u6307\u5b9a\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u306b\u30b3\u30d4\u30fc\u3055\u308c\u3001\u30b3\u30d4\u30fc\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u5229\u7528\u3057\u3066python\u30b3\u30de\u30f3\u30c9\u3084pip\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5f62\u306b\u306a\u308a\u307e\u3059\u3002
\n\u305d\u306e\u305f\u3081\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306epython\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

# \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\u306b\u79fb\u52d5\u3059\u308b\ncd {project_folder}\n\n# pyenv\u3067python\u304a\u3088\u3073pip\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\npyenv local 3.7.4\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\npip install --upgrade virtualenv\n\n# \u4eee\u60f3\u74b0\u5883\u69cb\u7bc9\n# - env\u306f\u5225\u306e\u540d\u524d\u3067\u3082OK\u3002env\u30d5\u30a9\u30eb\u30c0\u306bpython\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5168\u4f53\u306e\u30b3\u30d4\u30fc\u304c\u4f5c\u6210\u3055\u308c\u308b\nvirtualenv env\n\n# \u4eee\u60f3\u74b0\u5883\u6709\u52b9\u5316\n# - PATH\u306e\u4e00\u756a\u6700\u521d\u306b\u3001`env\/bin`\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\nsource env\/bin\/activate\n\n# python\u3084pip\u30b3\u30de\u30f3\u30c9\u304cenv\u914d\u4e0b\u306epython\u3092\u53c2\u7167\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308b\nwhich python\n> {project_folder}\/env\/bin\/python\n\n# \u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\n# - \u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u4ed6\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u306f\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\npip install -r requirements.txt\n\n# \u4eee\u60f3\u74b0\u5883\u7121\u52b9\u5316\n# - PATH\u304b\u3089`env\/bin`\u3092\u524a\u9664\u3057\u3066\u3044\u308b\ndeactivate<\/code><\/pre>\n

\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a<\/h2>\n

\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u7ba1\u7406\u306f\u3044\u308d\u3093\u306a\u3084\u308a\u65b9\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\n
python\u306b\u304a\u3051\u308b\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a<\/a><\/p>\n

\u578b\u30d2\u30f3\u30c8\u306b\u3064\u3044\u3066<\/h2>\n

\u578b\u30d2\u30f3\u30c8\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u9759\u7684\u578b\u4ed8\u8a00\u8a9e\u306e\u3088\u3046\u306a\u611f\u899a\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\n
python\u306e\u578b\u30d2\u30f3\u30c8<\/a><\/p>\n

\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u306eimport<\/h2>\n

\u89aa\u5144\u5f1f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308b\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092import\u3059\u308b\u5834\u5408\u3001\u7279\u5225\u306a\u5bfe\u5fdc\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\n
python\u306b\u304a\u3051\u308b\u89aa\/\u5144\u5f1f\u968e\u5c64\u306e\u30e2\u30b8\u30e5\u30fc\u30ebimport\u306e\u65b9\u6cd5<\/a><\/p>\n

\u5909\u6570\u30b9\u30b3\u30fc\u30d7<\/h2>\n

\u3053\u3061\u3089<\/a>\u306e\u8a18\u4e8b\u3067\u52c9\u5f37\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u306e\u3067\u3059\u304c\u3001python\u306e\u30b9\u30b3\u30fc\u30d7\u306f4\u6bb5\u968e\u3042\u308b\u3088\u3046\u3067\u3059\u3002for<\/code>\u3084if<\/code>\u306b\u30d6\u30ed\u30c3\u30af\u30b9\u30b3\u30fc\u30d7\u304c\u7121\u3044\u3053\u3068\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n