본문 바로가기
프로그램

[파이썬] Anaconda재설치, 가상환경에서 tensorflow 설치

by 오디세이99 2022. 8. 16.
728x90
반응형

그동안 파이썬이나 Anaconda의 가상환경을 사용하지 않고 있었습니다.

그러니까 파이썬, Cuda, tensorflow 등의 버전 충돌 및 환경의 차이로 여러번 에러가 발생했었습니다.

 

이번에도 matplotlib를 upgrade 했는데, 갑자기 tensorflow를 사용하는 프로그램이 동작을 하지 않게 되었습니다.

정확히 원인이 matplotlib인지도 모르겠지만...

에러 관련 자료를 찾아보았지만 마땅한 내용을 찾지 못했습니다.

 

드디어 한번 환경을 싹 바꿀때가 된 것 같습니다.

 

인터넷에서 찾아보면 Anaconda 및 tensorflow등을 설치하는 내용이 있습니다.

하지만 예전 내용와 최신 내용이 다릅니다. 그래서 저도 최신이고 저의 환경에서 설치한 내용을 기록해 봅니다.

 

 

 

기존 환경 삭제

Anaconda 삭제

- Anaconda 폴더에 uninstall_anaconda.exe 실행해서 삭제 했습니다.

 

Python 삭제

- Python도 2.6 버전부터 사용했었기 때문에 기존에 삭제했어도 설치 프로그램에 남아 있는 것도 있어서  레지스트리를 뒤져서 삭제 했습니다.

 

CUDA는 삭제하지 않았습니다.

 

 

 

설치

- Anaconda 설치

  'Advanced Options'가 Disable 되어 있네요. 기존에 PATH가 설정되어 있어서 그런지는 모르겠네요.

 

conda 환경변수 설정

- 기존에 path에 이 내용이 있었는데, Anaconda uninstall 시 제거된 것으로 보입니다.

새로 설정해 줍니다.

 

 

conda 설치 후 version 확인

- CUDA는 새로 설치하지 않았습니다. cuda 버전을 확인해 봅니다.

E:\RnD>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Nov_30_19:15:10_Pacific_Standard_Time_2020
Cuda compilation tools, release 11.2, V11.2.67
Build cuda_11.2.r11.2/compiler.29373293_0

 

conda 가상화 만들기

- conda 설치 후 'env001' 라는 이름으로 가상화를 만듭니다.

 

 

가상화 리스트를 확인해 봅니다. 새로만든 env001 이 없습니다. 위 결과도 다시보니 가상화가 만들어지지 않고 에러가 발생했습니다.

 

- conda를 update 했습니다.

 

이렇게 하면 문제가 해결된다는 내용을 찾았습니다.

E:\RnD>conda config --set ssl_verify no

 

 

E:\RnD>conda activate env001

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

 

 

현재 프롬프트 창을 닫고 새로 창을 열었습니다.

가상화를 만듭니다.

E:\RnD>conda create -n env001
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.12.0
  latest version: 4.13.0

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: C:\Users\admin\.conda\envs\env001



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate env001
#
# To deactivate an active environment, use
#
#     $ conda deactivate

 

가상화 환경 들어가기. 프롬프트 앞에 가상화명(env001)이 뜨는 것을 볼 수 있습니다.

이 가상화는 아무 Package도 없습니다. 다시 설치 해야 합니다.

E:\RnD>conda activate env001

(env001) E:\RnD>

 

cudatoolkit 을 설치 합니다.

(env001) E:\RnD>conda install -c anaconda cudatoolkit
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.12.0
  latest version: 4.13.0

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: C:\Users\admin\.conda\envs\env001

  added / updated specs:
    - cudatoolkit


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cudatoolkit-11.3.1         |       h59b6b97_2       820.7 MB  anaconda
    vc-14.2                    |       h21ff451_1           8 KB  anaconda
    vs2015_runtime-14.27.29016 |       h5e58377_2         2.2 MB  anaconda
    ------------------------------------------------------------
                                           Total:       822.9 MB

The following NEW packages will be INSTALLED:

  cudatoolkit        anaconda/win-64::cudatoolkit-11.3.1-h59b6b97_2
  vc                 anaconda/win-64::vc-14.2-h21ff451_1
  vs2015_runtime     anaconda/win-64::vs2015_runtime-14.27.29016-h5e58377_2


Proceed ([y]/n)? y


Downloading and Extracting Packages
vc-14.2              | 8 KB      | ################################################################################################################## | 100%
cudatoolkit-11.3.1   | 820.7 MB  | ################################################################################################################## | 100%
vs2015_runtime-14.27 | 2.2 MB    | ################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

 

현재 가상환경에 설치된 Package들을 리스트 해 확인 합니다.

(env001) E:\RnD>conda list
# packages in environment at C:\Users\admin\.conda\envs\env001:
#
# Name                    Version                   Build  Channel
cudatoolkit               11.3.1               h59b6b97_2    anaconda
vc                        14.2                 h21ff451_1    anaconda
vs2015_runtime            14.27.29016          h5e58377_2    anaconda

 

conda를 update 합니다.  설치 중 에러가 발생 했습니다.

E:\RnD\Stock\stockTradeClassify>conda update conda
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.12.0
  latest version: 4.13.0

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: E:\PGM\Anaconda3

  added / updated specs:
    - conda


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    automat-20.2.0             |             py_0          30 KB  conda-forge
    brotli-1.0.9               |       ha925a31_2         887 KB  conda-forge
    conda-4.13.0               |   py39hcbf5309_1         1.0 MB  conda-forge
    conda-pack-0.7.0           |     pyh6c4a22f_0          30 KB  conda-forge
    conda-package-handling-1.8.1|   py39hb3671d1_1         760 KB  conda-forge
    conda-repo-cli-1.0.5       |   py39haa95532_0         122 KB
    freetype-2.10.4            |       hd328e21_0         489 KB  conda-forge
    libzopfli-1.0.3            |       ha925a31_0         202 KB  conda-forge
    m2w64-libwinpthread-git-5.0.0.4634.697f757|                2          31 KB  conda-forge
    msys2-conda-epoch-20160418 |                1           3 KB  conda-forge
    pathlib-1.0.1              |   py39hcbf5309_6           5 KB  conda-forge
    pyjwt-2.4.0                |     pyhd8ed1ab_0          19 KB  conda-forge
    python_abi-3.9             |           2_cp39           4 KB  conda-forge
    winpty-0.4.3               |                4         1.1 MB  conda-forge
    yaml-0.2.5                 |       he774522_0          61 KB  conda-forge
    ------------------------------------------------------------
                                           Total:         4.7 MB

The following NEW packages will be INSTALLED:

  pathlib            conda-forge/win-64::pathlib-1.0.1-py39hcbf5309_6
  python_abi         conda-forge/win-64::python_abi-3.9-2_cp39

The following packages will be UPDATED:

  conda              pkgs/main::conda-4.12.0-py39haa95532_0 --> conda-forge::conda-4.13.0-py39hcbf5309_1
  conda-pack         pkgs/main::conda-pack-0.6.0-pyhd3eb1b~ --> conda-forge::conda-pack-0.7.0-pyh6c4a22f_0
  conda-package-han~ pkgs/main::conda-package-handling-1.8~ --> conda-forge::conda-package-handling-1.8.1-py39hb3671d1_1
  conda-repo-cli     pkgs/main/noarch::conda-repo-cli-1.0.~ --> pkgs/main/win-64::conda-repo-cli-1.0.5-py39haa95532_0
  pyjwt              pkgs/main/win-64::pyjwt-2.1.0-py39haa~ --> conda-forge/noarch::pyjwt-2.4.0-pyhd8ed1ab_0

The following packages will be SUPERSEDED by a higher-priority channel:

  automat                                         pkgs/main --> conda-forge
  brotli                                          pkgs/main --> conda-forge
  freetype                                        pkgs/main --> conda-forge
  libzopfli                                       pkgs/main --> conda-forge
  m2w64-libwinpthre~                             pkgs/msys2 --> conda-forge
  msys2-conda-epoch                              pkgs/msys2 --> conda-forge
  winpty                                          pkgs/main --> conda-forge
  yaml                                            pkgs/main --> conda-forge


Proceed ([y]/n)? y


Downloading and Extracting Packages
winpty-0.4.3         | 1.1 MB    | ################################################################################################################## | 100%
freetype-2.10.4      | 489 KB    | ################################################################################################################## | 100%
conda-pack-0.7.0     | 30 KB     | ################################################################################################################## | 100%
pyjwt-2.4.0          | 19 KB     | ################################################################################################################## | 100%
automat-20.2.0       | 30 KB     | ################################################################################################################## | 100%
msys2-conda-epoch-20 | 3 KB      | ################################################################################################################## | 100%
python_abi-3.9       | 4 KB      | ################################################################################################################## | 100%
conda-4.13.0         | 1.0 MB    | ################################################################################################################## | 100%
libzopfli-1.0.3      | 202 KB    | ################################################################################################################## | 100%
yaml-0.2.5           | 61 KB     | ################################################################################################################## | 100%
conda-repo-cli-1.0.5 | 122 KB    | ################################################################################################################## | 100%
brotli-1.0.9         | 887 KB    | ################################################################################################################## | 100%
pathlib-1.0.1        | 5 KB      | ################################################################################################################## | 100%
conda-package-handli | 760 KB    | ################################################################################################################## | 100%
m2w64-libwinpthread- | 31 KB     | ################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: failed

EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
  environment location: E:\PGM\Anaconda3

 

 

관리자 권한으로 '명령프롬프트'를 엽니다.

conda를 update 합니다. 가상환경이 아닌 그냥 상태에서 update 했습니다.

> conda update conda

설치된 Package를 리스트해서 확인 합니다.

E:\RnD>conda list
# packages in environment at E:\PGM\Anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0            py39haa95532_0
aiohttp                   3.8.1            py39h2bbff1b_1
aiosignal                 1.2.0              pyhd3eb1b0_0
alabaster                 0.7.12             pyhd3eb1b0_0
anaconda                  2022.05                  py39_0
anaconda-client           1.9.0            py39haa95532_0
anaconda-navigator        2.1.4            py39haa95532_0
anaconda-project          0.10.2             pyhd3eb1b0_0
....
conda                     4.13.0           py39hcbf5309_1    conda-forge
conda-build               3.21.8           py39haa95532_2
conda-content-trust       0.1.1              pyhd3eb1b0_0
conda-env                 2.6.0                haa95532_1
conda-pack                0.7.0              pyh6c4a22f_0    conda-forge
conda-package-handling    1.8.1            py39hb3671d1_1    conda-forge
conda-repo-cli            1.0.5            py39haa95532_0
conda-token               0.3.0              pyhd3eb1b0_0
conda-verify              3.4.2                      py_1

jupyter                   1.0.0            py39haa95532_7
jupyter_client            6.1.12             pyhd3eb1b0_0
jupyter_console           6.4.0              pyhd3eb1b0_0
jupyter_core              4.9.2            py39haa95532_0
jupyter_server            1.13.5             pyhd3eb1b0_0
jupyterlab                3.3.2              pyhd3eb1b0_0
jupyterlab_pygments       0.1.2                      py_0
jupyterlab_server         2.10.3             pyhd3eb1b0_1
jupyterlab_widgets        1.0.0              pyhd3eb1b0_1

matplotlib                3.5.1            py39haa95532_1
matplotlib-base           3.5.1            py39hd77b12b_1
matplotlib-inline         0.1.2              pyhd3eb1b0_2

pandas                    1.4.2            py39hd77b12b_0

python                    3.9.12               h6244533_0
python-dateutil           2.8.2              pyhd3eb1b0_0
python-fastjsonschema     2.15.1             pyhd3eb1b0_0
python-libarchive-c       2.9                pyhd3eb1b0_1
python-lsp-black          1.0.0              pyhd3eb1b0_0
python-lsp-jsonrpc        1.0.0              pyhd3eb1b0_0
python-lsp-server         1.2.4              pyhd3eb1b0_0
python-slugify            5.0.2              pyhd3eb1b0_0
python-snappy             0.6.0            py39hd77b12b_3
python_abi                3.9                      2_cp39    conda-forge
...

 

일반 명령프롬프티에서 가상환경에서 python 을 설치 합니다.

(env001) E:\RnD> conda install python

 

python 버전을 확인해봅니다.

(env001) E:\RnD>python --version
Python 3.10.5

 

matplotlib를 install 합니다.

(env001) E:\RnD> conda install matplotlib

 

tensorflow를 사용하는 코드의 프로그램 실행해 보니 설치가 않된 Package들에서 에러가 발생합니다.

(env001) E:\RnD\>python exKeras.py
Traceback (most recent call last):
  File "E:\RnD\exKeras.py", line 4, in <module>
    import seaborn as sns
ModuleNotFoundError: No module named 'seaborn'

관련 package들을 설치 합니다.

(env001) E:\RnD>conda instll seaborn


(env001) E:\RnD>conda instll pandas-datareader


(env001) E:\RnD>conda install scikit-learn

 

tensorflow를 설치 했는데, python version과 맞지 않는 것 같습니다. 대부분 tensorflow가 python 버전과 맞는지 확인 해야 합니다.

(env001) E:\RnD\Stock\stockTradeClassify>conda install tensorflow
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: /
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.-
failed

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - tensorflow -> python[version='3.5.*|3.6.*|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|3.8.*|3.7.*|3.9.*']

Your python: python=3.10

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

 

설치되어 있던 python을 삭제 하고, 버전을 지정해서 다시 설치 합니다.

그리고 tensorflow를 설치 합니다.

(env001) E:\RnD> conda remove python

(env001) E:\RnD> conda install python=3.9

(env001) E:\RnD> conda install tensorflow
(env001) E:\RnD\Stock\stockTradeClassify>conda install tensorflow
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\admin\.conda\envs\env001

  added / updated specs:
    - tensorflow


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _tflow_select-2.3.0        |              mkl           3 KB
    abseil-cpp-20210324.2      |       h0e60522_0         2.1 MB  conda-forge
    absl-py-1.2.0              |     pyhd8ed1ab_0          94 KB  conda-forge
    aiohttp-3.8.1              |   py39hb82d6ee_1         545 KB  conda-forge
    aiosignal-1.2.0            |     pyhd8ed1ab_0          12 KB  conda-forge
    astor-0.8.1                |     pyh9f0ad1d_0          25 KB  conda-forge
    .....
    wrapt-1.14.1               |   py39hb82d6ee_0          49 KB  conda-forge
    yarl-1.7.2                 |   py39hb82d6ee_2         127 KB  conda-forge
    zipp-3.8.1                 |     pyhd8ed1ab_0          13 KB  conda-forge
    ------------------------------------------------------------
                                           Total:       179.0 MB

The following NEW packages will be INSTALLED:

  _tflow_select      pkgs/main/win-64::_tflow_select-2.3.0-mkl
  abseil-cpp         conda-forge/win-64::abseil-cpp-20210324.2-h0e60522_0
  absl-py            conda-forge/noarch::absl-py-1.2.0-pyhd8ed1ab_0
  aiohttp            conda-forge/win-64::aiohttp-3.8.1-py39hb82d6ee_1
  aiosignal          conda-forge/noarch::aiosignal-1.2.0-pyhd8ed1ab_0
  astor              conda-forge/noarch::astor-0.8.1-pyh9f0ad1d_0
  ....
  tensorflow         pkgs/main/win-64::tensorflow-2.6.0-mkl_py39h31650da_0
  tensorflow-base    pkgs/main/win-64::tensorflow-base-2.6.0-mkl_py39h9201259_0
  tensorflow-estima~ pkgs/main/noarch::tensorflow-estimator-2.6.0-pyh7b7c402_0
  termcolor          conda-forge/noarch::termcolor-1.1.0-pyhd8ed1ab_3
  typing-extensions  conda-forge/noarch::typing-extensions-4.3.0-hd8ed1ab_0
  typing_extensions  conda-forge/noarch::typing_extensions-4.3.0-pyha770c72_0
  urllib3            conda-forge/noarch::urllib3-1.26.11-pyhd8ed1ab_0
  werkzeug           conda-forge/noarch::werkzeug-2.2.2-pyhd8ed1ab_0
  win_inet_pton      conda-forge/win-64::win_inet_pton-1.1.0-py39hcbf5309_4
  wrapt              conda-forge/win-64::wrapt-1.14.1-py39hb82d6ee_0
  yarl               conda-forge/win-64::yarl-1.7.2-py39hb82d6ee_2
  zipp               conda-forge/noarch::zipp-3.8.1-pyhd8ed1ab_0
  zlib               conda-forge/win-64::zlib-1.2.12-h8ffe710_2

The following packages will be DOWNGRADED:

  openssl                                  3.0.5-h8ffe710_1 --> 1.1.1q-h8ffe710_0
  python                          3.9.13-hcf16a7b_0_cpython --> 3.9.13-h9a09f29_0_cpython
  wheel                                 0.37.1-pyhd8ed1ab_0 --> 0.35.1-pyh9f0ad1d_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
requests-oauthlib-1. | 22 KB     | ################################################################################################################## | 100%
google-auth-oauthlib | 19 KB     | ################################################################################################################## | 100%
google-pasta-0.2.0   | 42 KB     | ################################################################################################################## | 100%
....

 

pykrx package를 설치하는데 에러가 발생했습니다.

(env001) E:\RnD\Stock\stockTradeClassify>conda install pykrx
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:

  - pykrx

 

pip install로 설치 했습니다.

(env001) E:\RnD\Stock\stockTradeClassify>pip install pykrx
Collecting pykrx
  Downloading pykrx-1.0.37-py3-none-any.whl (97 kB)
     ---------------------------------------- 97.0/97.0 kB 5.4 MB/s eta 0:00:00
Collecting xlrd
  Downloading xlrd-2.0.1-py2.py3-none-any.whl (96 kB)
     ---------------------------------------- 96.5/96.5 kB 5.8 MB/s eta 0:00:00
Collecting deprecated
  Using cached Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting datetime
  Downloading DateTime-4.5-py2.py3-none-any.whl (52 kB)

 

tensorflow 를 사용하는 프로그램을 실행해보니 gpu 동작을 하지 않습니다.

기존에 설치된 tensorflow를 삭제하지는 않았습니다.

(env001) E:\>conda install -c anaconda tensorflow-gpu

 

장장 2일에 걸쳐 환경을 설정해서 드디어 완료!

 

728x90
반응형

댓글