갈루아의 반서재

아나콘다 가상환경에서 ccxt 패키지를 설치하였으나 주피터 노트북에서 아래와 같이 해당 모듈을 찾을 수 없다고 나오는 경우의 처리방법에 대해 알아보자.

import ccxt
print(ccxt.exchanges)
--------------------------------------------------------------------------- 
ModuleNotFoundError                       Traceback (most recent call last) 
 in () 
----> 1 import ccxt 
      2 print(ccxt.exchanges) 

ModuleNotFoundError: No module named 'ccxt'

하지만 아래에서 보는 바와 같이 ccxt 1.18.766 버전이 설치되어 있음을 알 수 있다.

(lindy) founder@casey:~$ pip list
Package          Version
---------------- ---------
aiodns           1.1.1
aiohttp          3.5.4
asn1crypto       0.24.0
async-timeout    3.0.1
attrs            19.1.0
autobahn         19.6.2
Automat          0.7.0
ccxt             1.18.766
certifi          2019.6.16
cffi             1.12.3
chardet          3.0.4
constantly       15.1.0
cryptography     2.7
dateparser       0.7.1

이렇게 터미널 실행시와 노트북에서 실행하는 경우 결과가 상이하게 나오는 경우에는 각각의 상황에서 실행경로를 살펴볼 필요가 있다.

먼저 터미널 환경을 보자. 파이썬과 pip 모두 현재 활성화된 lindy 가상환경상에 위치한 실행파일을 구동하고 있음을 알 수 있다.

[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/home/founder/anaconda3/envs/lindy/bin/python'
>>>

(lindy) founder@casey:~$ which pip
/home/founder/anaconda3/envs/lindy/bin/pip

이에 반해 주피터의 경우는 아나콘다 설치시 생성된 주피터를 실행하고 있음을 알 수 있다. 즉, ccxt 패키지는 lindy 가상환경에서 설치해놓은 반면, ccxt 가 설치되어 있지 않은 주피터에서 해당 패키지를 가져올려고 했으니 안되는 것이 당연하다. 

(lindy) founder@casey:~$ which jupyter
/home/founder/anaconda3/bin/jupyter

그러면 노트북에서 실행시 경로는 어떤지 살펴보자. 아래에서 보는 바와 같이 가상환경이 아닌 기본 아나콘다 경로이다. 

import sys
sys.executable
'/home/founder/anaconda3/bin/python'

!which pip
/home/founder/anaconda3/bin/pip

가상환경에서 주피터 노트북을 설치한다. 설치 후 주피터의 실행경로가 lindy 가상환경으로 변경되었음을 알 수 있다.

(lindy) founder@casey:~$ pip install jupyter
(lindy) founder@casey:~$ which jupyter
/home/founder/anaconda3/envs/lindy/bin/jupyter
import sys
sys.executable
'/home/founder/anaconda3/envs/lindy/bin/python'

!which pip
/home/founder/anaconda3/envs/lindy/bin/pip

이제 ccxt 패키지를 가져와보자. 정상적으로 작동됨을 알 수 있다.

import ccxt
print(ccxt.exchanges)

['_1btcxe', 'acx', 'allcoin', 'anxpro', 'anybits', 'bcex', 'bequant', 'bibox', 'bigone', 'binance', 'binanceje', 'bit2c', 'bitbank', 'bitbay', 'bitfinex', 'bitfinex2', 'bitflyer', 'bitforex', 'bithumb', 'bitibu', 'bitkk', 'bitlish', 'bitmarket', 'bitmex', 'bitsane', 'bitso', 'bitstamp', 'bitstamp1', 'bittrex', 'bitz', 'bl3p', 'bleutrade', 'braziliex', 'btcalpha', 'btcbox', 'btcchina', 'btcexchange', 'btcmarkets', 'btctradeim', 'btctradeua', 'btcturk', 'buda', 'bxinth', 'ccex', 'cex', 'chbtc', 'chilebit', 'cobinhood', 'coinbase', 'coinbaseprime', 'coinbasepro', 'coincheck', 'coinegg', 'coinex', 'coinexchange', 'coinfalcon', 'coinfloor', 'coingi', 'coinmarketcap', 'coinmate', 'coinnest', 'coinone', 'coinspot', 'cointiger', 'coolcoin', 'coss', 'crex24', 'crypton', 'deribit', 'dsx', 'dx', 'ethfinex', 'exmo', 'exx', 'fcoin', 'fcoinjp', 'flowbtc', 'foxbit', 'fybse', 'fybsg', 'gateio', 'gdax', 'gemini', 'getbtc', 'hadax', 'hitbtc', 'hitbtc2', 'huobipro', 'huobiru', 'ice3x', 'independentreserve', 'indodax', 'itbit', 'kkex', 'kraken', 'kucoin', 'kucoin2', 'kuna', 'lakebtc', 'lbank', 'liqui', 'liquid', 'livecoin', 'luno', 'lykke', 'mandala', 'mercado', 'mixcoins', 'negociecoins', 'nova', 'oceanex', 'okcoincny', 'okcoinusd', 'okex', 'okex3', 'paymium', 'poloniex', 'rightbtc', 'southxchange', 'stronghold', 'surbitcoin', 'theocean', 'therock', 'tidebit', 'tidex', 'uex', 'upbit', 'urdubit', 'vaultoro', 'vbtc', 'virwox', 'xbtce', 'yobit', 'zaif', 'zb']

 

참고링크 

Import on Jupyter notebook failed where command prompt works. https://github.com/jupyter/notebook/issues/1524
Conda environments not showing up in Jupyter Notebook https://stackoverflow.com/questions/39604271/conda-environments-not-showing-up-in-jupyter-notebook
Installing Jupyter Notebook https://jupyter.readthedocs.io/en/latest/install.html