python from import モジュール パッケージ

importの仕方

【import <モジュール名>】

モジュールは Python の関数やメソッドが詰め込まれているファイルとそれが集まったフォルダ。ファイル名はモジュール名に接尾語 .py がついたものになります。
import random
dice = random.choice([1,2,3])

【from <モジュール名> import <関数・変数名> 】

from <モジュール名> import <関数・変数名>
from random import choice
dice = choice([1,2,3])
(module.functionではなく直接function()で使える)
*モジュール内の他の関数は使えなくなります

例 pprint
辞書やリストを整形して出力してくれるpprint
pprintというモジュール内のpprintという関数なので使うときはpprint.pprint(list)
としなくてはいけないが、煩わしい。こんな時こそ
from pprint import pprintとすればpprint(list)で良い

【import <モジュール名> as エイリアス】

import random as rd
dice = rd.choice(list)


import してもファイルがないと怒られる。pip install してあるがな。
pip listでも出てくるしー???そんな時疑ってみよう、あなたはanaconda派?

Conda コマンド

condaとpipは混ぜるな危険

Python の setup.py を書いてみる

pip installでエラーになりインストールできない場合(Windows)

 

 

 

 


AnacondaはPIPを使っては駄目?

AnacondaはPythonから独立して環境を持っているので、Anacondaを使う場合はcondaを使う。

(PCにputhon3.*とAnacondaのpython3.*が両方でき、モジュールも別々に入る可能性あり)
かつてpython 3.7 【pip installはこちらにインストール】と
anaconda(python3.6) 【conda installはこちらにインストール

両方をインストールしていた時期があった。

結果ライブラリーがバラバラに入っている。

anacondaインストールパッケージ python単体インストール(uninstall済み)
conda 4.7.12  
Python 3.6.9 :: Anaconda, Inc. python 3.7
> conda list > pip list
# packages in environment at C:\develope\anaconda:  
# Name                    Version                   Build  Channel Package                       Version
aiodns                    1.1.1                    pypi_0    pypi aiodns                        1.1.1
aiohttp                   3.6.2                    pypi_0    pypi aiohttp                       3.6.2
alabaster                 0.7.12                   py36_0 alabaster                     0.7.12
anaconda-client           1.7.2                    py36_0 anaconda-client               1.7.2
anaconda-navigator        1.9.7                    py36_0 anaconda-navigator            1.9.7
asn1crypto                1.2.0                    py36_0 asn1crypto                    1.2.0
astroid                   2.3.3                    pypi_0    pypi astroid                       2.3.3
async-timeout             3.0.1                    pypi_0    pypi async-timeout                 3.0.1
attrs                     19.3.0                     py_0 attrs                         19.3.0
babel                     2.7.0                      py_0 Babel                         2.7.0
backcall                  0.1.0                    py36_0 backcall                      0.1.0
beautifulsoup4            4.8.1                    pypi_0    pypi beautifulsoup4                4.8.1
bitflyer                  0.0.2                    pypi_0    pypi bitflyer                      0.0.2
bleach                    3.1.0                    py36_0 bleach                        3.1.0
bs4                       0.0.1                    pypi_0    pypi bs4                           0.0.1
ca-certificates           2019.10.16                    0 cchardet                      2.1.5
cchardet                  2.1.5                    pypi_0    pypi ccxt                          1.19.72
ccxt                      1.19.72                  pypi_0    pypi certifi                       2019.9.11
certifi                   2019.9.11                py36_0 cffi                          1.13.1
cffi                      1.13.1           py36h7a1dbc1_0 chardet                       3.0.4
chardet                   3.0.4                 py36_1003 chromedriver                  2.24.1
chromedriver              2.24.1                   pypi_0    pypi Click                         7.0
click                     7.0                      pypi_0    pypi cloudpickle                   1.2.2
cloudpickle               1.2.2                      py_0 clyent                        1.2.2
clyent                    1.2.2                    py36_1 colorama                      0.4.1
colorama                  0.4.1                    py36_0 conda                         4.7.12
conda                     4.7.12                   py36_0 conda-package-handling        1.6.0
conda-package-handling    1.6.0            py36h62dcd97_0 cryptography                  2.8
cryptography              2.8              py36h7a1dbc1_0 Cython                        0.29.14
cython                    0.29.14                  pypi_0    pypi DateTime                      4.3
datetime                  4.3                      pypi_0    pypi decorator                     4.4.1
decorator                 4.4.1                      py_0 defusedxml                    0.6.0
defusedxml                0.6.0                      py_0 docutils                      0.15.2
docutils                  0.15.2                   py36_0 entrypoints                   0.3
entrypoints               0.3                      py36_0 future                        0.18.2
freetype                  2.9.1                ha9979f8_1 hyperopt                      0.2.2
future                    0.18.2                   pypi_0    pypi idna                          2.8
git                       2.23.0               h6bb4b03_0 idna-ssl                      1.0.1
hyperopt                  0.2.2                    pypi_0    pypi imagesize                     1.1.0
icu                       58.2                 ha66f8fd_1 importlib-metadata            0.23
idna                      2.8                      py36_0 ipykernel                     5.1.3
idna-ssl                  1.0.1                    pypi_0    pypi ipython                       7.9.0
imagesize                 1.1.0                    py36_0 ipython-genutils              0.2.0
importlib_metadata        0.23                     py36_0 isort                         4.3.21
ipykernel                 5.1.3            py36h39e3cac_0 jedi                          0.15.1
ipython                   7.9.0            py36h39e3cac_0 Jinja2                        2.10.3
ipython_genutils          0.2.0            py36h3c5d0ee_0 jsonschema                    3.2.0
isort                     4.3.21                   py36_0 jupyter-client                5.3.4
jedi                      0.15.1                   py36_0 jupyter-core                  4.6.1
jinja2                    2.10.3                     py_0 keyring                       19.2.0
jpeg                      9b                   hb83a4c4_2 lazy-object-proxy             1.4.3
jsonschema                3.2.0                    pypi_0    pypi MarkupSafe                    1.1.1
jupyter_client            5.3.4                    py36_0 mccabe                        0.6.1
jupyter_core              4.6.1                    py36_0 menuinst                      1.4.16
keyring                   19.2.0                   pypi_0    pypi mistune                       0.8.4
lazy-object-proxy         1.4.3            py36he774522_0 MonthDelta                    0.9.1
libpng                    1.6.37               h2a8f88b_0 more-itertools                7.2.0
libsodium                 1.0.16               h9d3ae62_0 mpl-finance                   0.10.0
libtiff                   4.0.10               hb898794_2 msgpack                       0.6.2
m2w64-gcc-libgfortran     5.3.0                         6 multidict                     4.6.1
m2w64-gcc-libs            5.3.0                         7 nbconvert                     5.6.1
m2w64-gcc-libs-core       5.3.0                         7 nbformat                      4.4.0
m2w64-gmp                 6.1.0                         2 network                       0.1
m2w64-libwinpthread-git   5.0.0.4634.697f757               2 networkx                      2.4
markupsafe                1.1.1            py36he774522_0 notebook                      6.0.2
mccabe                    0.6.1                    py36_1 numpy                         1.17.4
menuinst                  1.4.16           py36he774522_0 numpydoc                      0.9.1
mistune                   0.8.4            py36he774522_0 olefile                       0.46
monthdelta                0.9.1                    pypi_0    pypi packaging                     19.2
more-itertools            7.2.0                    py36_0 pandas                        0.25.3
mpl-finance               0.10.0                   pypi_0    pypi pandoc                        1.0.2
msgpack                   0.6.2                    pypi_0    pypi pandocfilters                 1.4.2
msys2-conda-epoch         20160418                      1 parso                         0.5.1
multidict                 4.6.1                    pypi_0    pypi pickleshare                   0.7.5
nbconvert                 5.6.1                    py36_0 Pillow                        6.2.1
nbformat                  4.4.0            py36h3a5bc1b_0 pip                           19.3.1
network                   0.1                      pypi_0    pypi pip-tools                     4.2.0
networkx                  2.4                      pypi_0    pypi prometheus-client             0.7.1
notebook                  6.0.2                    pypi_0    pypi prompt-toolkit                2.0.10
numpy                     1.17.4                   pypi_0    pypi psutil                        5.6.5
numpydoc                  0.9.1                      py_0 pubnub                        4.1.6
olefile                   0.46                     py36_0 pybitflyer                    0.1.9
openssl                   1.1.1d               he774522_3 pycares                       3.0.0
packaging                 19.2                       py_0 pycodestyle                   2.5.0
pandas                    0.25.3                   pypi_0    pypi pycosat                       0.6.3
pandoc                    1.0.2                    pypi_0    pypi pycparser                     2.19
pandocfilters             1.4.2                    py36_1 pycryptodomex                 3.9.4
parso                     0.5.1                      py_0 pyflakes                      2.1.1
pickleshare               0.7.5                    py36_0 pygame                        1.9.6
pillow                    6.2.1            py36hdc69c19_0 Pygments                      2.4.2
pip                       19.1.1                   pypi_0    pypi pylint                        2.4.4
pip-tools                 4.2.0                    pypi_0    pypi pymongo                       3.9.0
prometheus_client         0.7.1                      py_0 pyOpenSSL                     19.1.0
prompt_toolkit            2.0.10                     py_0 pyparsing                     2.4.5
psutil                    5.6.5                    pypi_0    pypi pyrsistent                    0.15.4
pubnub                    4.1.6                    pypi_0    pypi PySocks                       1.7.1
pybitflyer                0.1.9                    pypi_0    pypi pystan                        2.19.1.1
pycares                   3.0.0                    pypi_0    pypi python-dateutil               2.8.1
pycodestyle               2.5.0                    py36_0 pytils                        0.3
pycosat                   0.6.3            py36hfa6e2cd_0 pytz                          2019.3
pycparser                 2.19                     py36_0 pywin32                       225
pycryptodomex             3.9.4                    pypi_0    pypi pywin32-ctypes                0.2.0
pyflakes                  2.1.1                    py36_0 pywinpty                      0.5.5
pygame                    1.9.6                    pypi_0    pypi PyYAML                        5.1.2
pygments                  2.4.2                      py_0 pyzmq                         18.1.1
pylint                    2.4.4                    pypi_0    pypi QtAwesome                     0.6.0
pymongo                   3.9.0                    pypi_0    pypi qtconsole                     4.6.0
pyopenssl                 19.1.0                   pypi_0    pypi QtPy                          1.9.0
pyparsing                 2.4.5                    pypi_0    pypi requests                      2.22.0
pyqt                      5.9.2            py36h6538335_2 response                      0.4.0
pyrsistent                0.15.4           py36he774522_0 rope                          0.14.0
pysocks                   1.7.1                    py36_0 ruamel-yaml                   0.15.46
pystan                    2.19.1.1                 pypi_0    pypi scipy                         1.3.2
python                    3.6.9                h5500b2f_0 selectors                     0.0.14
python-dateutil           2.8.1                    pypi_0    pypi selenium                      3.141.0
pytils                    0.3                      pypi_0    pypi Send2Trash                    1.5.0
pytz                      2015.7                   pypi_0    pypi setuptools                    41.6.0.post20191030
pywin32                   225                      pypi_0    pypi simplejson                    3.17.0
pywin32-ctypes            0.2.0                    pypi_0    pypi six                           1.12.0
pywinpty                  0.5.5                 py36_1000 snowballstemmer               2.0.0
pyyaml                    5.1.2            py36he774522_0 soupsieve                     1.9.5
pyzmq                     18.1.1                   pypi_0    pypi Sphinx                        2.2.1
qt                        5.9.7            vc14h73c81de_0 sphinxcontrib-applehelp       1.0.1
qtawesome                 0.6.0                      py_0 sphinxcontrib-devhelp         1.0.1
qtconsole                 4.6.0                    pypi_0    pypi sphinxcontrib-htmlhelp        1.0.2
qtpy                      1.9.0                      py_0 sphinxcontrib-jsmath          1.0.1
requests                  2.22.0                   py36_0 sphinxcontrib-qthelp          1.0.2
response                  0.4.0                    pypi_0    pypi sphinxcontrib-serializinghtml 1.1.3
rope                      0.14.0                     py_0 spyder                        3.3.6
ruamel_yaml               0.15.46          py36hfa6e2cd_0 spyder-kernels                1.8.0
scipy                     1.3.2                    pypi_0    pypi terminado                     0.8.2
selectors                 0.0.14                   pypi_0    pypi testpath                      0.4.4
selenium                  3.141.0                  pypi_0    pypi tornado                       6.0.3
send2trash                1.5.0                    py36_0 tqdm                          4.39.0
setuptools                41.6.0                   py36_0 traitlets                     4.3.3
simplejson                3.17.0                   pypi_0    pypi typed-ast                     1.4.0
sip                       4.19.8           py36h6538335_0 typing-extensions             3.7.4.1
six                       1.12.0                   py36_0 urllib3                       1.25.7
snowballstemmer           2.0.0                      py_0 user-agent                    0.1.9
soupsieve                 1.9.5                    pypi_0    pypi wait                          0.0.3
sphinx                    2.2.1                      py_0 wcwidth                       0.1.7
sphinxcontrib-applehelp   1.0.1                      py_0 webencodings                  0.5.1
sphinxcontrib-devhelp     1.0.1                      py_0 weblib                        0.1.30
sphinxcontrib-htmlhelp    1.0.2                      py_0 websocket-client              0.56.0
sphinxcontrib-jsmath      1.0.1                      py_0 wheel                         0.33.6
sphinxcontrib-qthelp      1.0.2                      py_0 win-inet-pton                 1.1.0
sphinxcontrib-serializinghtml 1.1.3                      py_0 wincertstore                  0.2
spyder                    3.3.6                    py36_0 wrapt                         1.11.2
spyder-kernels            1.8.0                    pypi_0    pypi yarl                          1.1.0
sqlite                    3.30.1               he774522_0 zipp                          0.6.0
terminado                 0.8.2                    py36_0 zope.interface                4.7.1
testpath                  0.4.4                    pypi_0    pypi  
tk                        8.6.8                hfa6e2cd_0  
tornado                   6.0.3            py36he774522_0  
tqdm                      4.39.0                   pypi_0    pypi  
traitlets                 4.3.3                    py36_0  
typed-ast                 1.4.0            py36he774522_0  
typing-extensions         3.7.4.1                  pypi_0    pypi  
urllib3                   1.25.7                   pypi_0    pypi  
user-agent                0.1.9                    pypi_0    pypi  
vc                        14.1                 h0510ff6_4  
vs2015_runtime            14.16.27012          hf0eaf9b_0  
wait                      0.0.3                    pypi_0    pypi  
wcwidth                   0.1.7            py36h3d5aa90_0  
webencodings              0.5.1                    py36_1  
weblib                    0.1.30                   pypi_0    pypi  
websocket-client          0.56.0                   pypi_0    pypi  
wheel                     0.33.6                   py36_0  
win_inet_pton             1.1.0                    py36_0  
wincertstore              0.2              py36h7fe50ca_0  
winpty                    0.4.3                         4  
wrapt                     1.11.2           py36he774522_0  
xz                        5.2.4                h2fa13f4_4  
yaml                      0.1.7                hc54c509_2  
yarl                      1.1.0                    pypi_0    pypi  
zeromq                    4.3.1                h33f27b4_3  
zipp                      0.6.0                      py_0  
zlib                      1.2.11               h62dcd97_3  
zope-interface            4.7.1                    pypi_0    pypi  
zstd                      1.3.7                h508b16e_0  

 


__pycache__ (パイキャッシュ)

pythonのインストールフォルダ下のlibフォルダを見ると

pip install されたライブラリがたくさんある。

そのフォルダの下に__pychash__というフォルダが、ほぼならずある

一回目に呼び出された時に、コンパイルされて中間コードが置かれるそうだ。

なるほど、LINKERが要らないで同等のことをしてくれるあたりが、python人気の秘密なんですね。