[Python Tips] Anaconda 가상 환경을 사용하여 새 버전의 Python에서 지정된 스크립트를 실행할 수 없는 문제를 해결하고 Notepad++를 구현하여 클릭 한 번으로 Python Anaconda 가상 환경을 실행합니다.


머리말

저자는 오늘 이상한 문제에 직면했는데, Python을 3.8에서 3.10으로 업그레이드한 후 잘 작동하고 있습니다. 하지만 오늘 특정 기능을 실행하면 항상 응답 없이 종료됩니다. 처음에는 pandas1.5.3을 2.0.0으로 업그레이드하는 문제인 줄 알았는데 당시 2.0pandas가 빠르게 실행되고 있어서 업그레이드를 하였으니 일단은 배제하자. 먼저 팬더를 최신 버전으로 업그레이드하세요. 버전 1.5.3으로 떨어뜨린 후에도 여전히 작동하지 않습니다. 동일한 코드를 Python3.8.16을 사용하여 다른 컴퓨터로 변경할 수 있으며 이 팬더를 최신 버전으로 업그레이드해도 괜찮습니다.

문득 파이썬 버전의 문제일지도 모른다는 생각이 들었습니다.

Anaconda는 이러한 난처함을 해결하기 위해 가상 환경을 지원하므로 더 낮은 버전의 Python 환경을 만들까 생각했습니다. 아래에서 해보지만 환경을 설치한 후에도 아직 별도로 설치해야 하는 라이브러리가 많으므로 나중에 부록으로 정리하도록 하겠습니다.

1. 가상 환경 생성

Anaconda는 가상 환경을 생성하고 관리하는 데 매우 편리하며 여러 Python 버전이 필요한 경우 conda 명령줄을 사용하여 다양한 가상 환경을 생성할 수 있습니다. 구체적인 단계는 다음과 같습니다.

  1. 생성하기 전에 현재 사용 가능한 가상 환경이 무엇인지 살펴보겠습니다.
conda info --envs

일반적으로 Anaconda가 설치되면 기본적으로 기본 환경이 있습니다. 결과는 다음과 같습니다.

(base) C:\Users\Administrator>conda info --envs
# conda environments:
#
base                  *  d:\ProgramData\anaconda3
  1. python38 python=3.8.16 환경 만들기
conda create --name python38 python=3.8.16
  1. 환경 활성화
conda activate python38
  1. 환경 초기화(이 단계는 매우 중요합니다. 인터넷에 일반 자습서가 없습니다. 이 단계를 수행하지 않으면 나중에 notepad++로 Python을 실행할 때 오류가 보고됩니다.)
conda init

다음 프롬프트가 나타나면 init 명령을 실행하십시오.

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'.

기타 관련 명령:
입력: conda list, 현재 가상 환경에 포함된 패키지를 확인할 수 있습니다.
입력: conda deactivate, 현재 활성화된 환경 종료 입력
: conda create --name new_env_name–clone old_env_name 복제하여 환경 사본 생성 입력
: conda rename -n old_name new_name, rename으로 환경 이름 바꾸기 입력
: conda remove -n env_name --all -y 지정된 이름으로 가상 환경 삭제

2. Python 가상 환경을 실행하기 위한 단축키 구성 Nodepad++

  1. 위에서 생성한 환경을 python38이라 부르기 때문에 명령어에 bat 파일을 만들기 위해서는 먼저 conda activate python38을 실행한다.

예를 들어 D:\ProgramData\anaconda3\Scripts\ 디렉토리에 activatepy38.bat를 생성하고 콘텐츠는 단 한 줄입니다.

conda activate python38
  1. Notepad++를 열고 F5 키를 눌러 실행한 다음 입력 상자에 다음 명령을 붙여넣고 저장을 클릭한 다음 바로 가기 키를 선택합니다. 여기서는 Alt+E로 설정되어 있습니다. 그런 다음 확인을 클릭합니다.
cmd /k chdir /d $(CURRENT_DIRECTORY) & call D:\ProgramData\anaconda3\Scripts\activatepy38.bat & python $(FILE_NAME) & PAUSE & EXIT

다른 버전이 있으면 해당 이름으로 바꿉니다.


요약하다

오늘은 오랫동안 작은 기능을 던지고 있으며 Ta-lib 라이브러리 및 이전 휠 보물 창고를 설치할 때 여전히 오류가 보고됩니다: https://www.lfd.uci.edu/~gohlke/ pythonlibs를 다운로드할 수 없습니다. 다행스럽게도 Stock을 찾았습니다. 드디어 끝났습니다.

트렌드를 따라가는 것도 사실이지만, 각 버전의 파이썬이 안정적으로 동작하는 것이 맞아떨어져야 한다는 점을 여기서 말해야겠다. 테스트를 하셔도 상관없지만, 작업 환경으로서 더욱 주의하셔야 합니다!

부록


(base) C:\Users\Administrator>conda create --name python38 python=3.8.16

(base) C:\Users\Administrator>conda activate python38

(python38) C:\Users\Administrator>conda info --envs
# conda environments:
#
base                     d:\ProgramData\anaconda3
python38              *  d:\ProgramData\anaconda3\envs\python38

(python38) C:\Users\Administrator>pyhton
'pyhton' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

(python38) C:\Users\Administrator>python
Python 3.8.16 (default, Mar  2 2023, 03:18:16) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

(python38) C:\Users\Administrator>pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pandas
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/41/77/a8210fab9a40a3546ab24f69e81c77539818d4379b6255a4510892d91015/pandas-2.0.1-cp38-cp38-win_amd64.whl (10.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.8/10.8 MB 1.4 MB/s eta 0:00:00
Collecting pytz>=2020.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/99/ad6bd37e748257dd70d6f85d916cafe79c0b0f5e2e95b11f7fbc82bf3110/pytz-2023.3-py2.py3-none-any.whl (502 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 502.3/502.3 kB 926.8 kB/s eta 0:00:00
Collecting numpy>=1.20.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1a/62/af7e78a12207608b23e3b2e248fc823fbef75f17d5defc8a127c5661daca/numpy-1.24.3-cp38-cp38-win_amd64.whl (14.9 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.9/14.9 MB 302.9 kB/s eta 0:00:00
Collecting python-dateutil>=2.8.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 460.2 kB/s eta 0:00:00
Collecting tzdata>=2022.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d5/fb/a79efcab32b8a1f1ddca7f35109a50e4a80d42ac1c9187ab46522b2407d7/tzdata-2023.3-py2.py3-none-any.whl (341 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 341.8/341.8 kB 451.8 kB/s eta 0:00:00
Collecting six>=1.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, tzdata, six, numpy, python-dateutil, pandas
Successfully installed numpy-1.24.3 pandas-2.0.1 python-dateutil-2.8.2 pytz-2023.3 six-1.16.0 tzdata-2023.3


(python38) C:\Users\Administrator>pip install Ta-lib -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting Ta-lib
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/39/6f/6acaee2eac6afb2cc6a2adcb294080577f9983fbd2726395b9047c4e13ec/TA-Lib-0.4.26.tar.gz (272 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 272.6/272.6 kB 1.4 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: numpy in d:\programdata\anaconda3\envs\python38\lib\site-packages (from Ta-lib) (1.24.3)
Building wheels for collected packages: Ta-lib
  Building wheel for Ta-lib (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for Ta-lib (pyproject.toml) did not run successfully.exit code: 1
  ╰─> [20 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-38
      creating build\lib.win-amd64-cpython-38\talib
      copying talib\abstract.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\deprecated.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\stream.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_abstract.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_data.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_func.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_pandas.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_polars.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\test_stream.py -> build\lib.win-amd64-cpython-38\talib
      copying talib\__init__.py -> build\lib.win-amd64-cpython-38\talib
      running build_ext
      building 'talib._ta_lib' extension
      <string>:77: UserWarning: Cannot find ta-lib library, installation may fail.
      error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for Ta-lib
Failed to build Ta-lib
ERROR: Could not build wheels for Ta-lib, which is required to install pyproject.toml-based projects

(python38) C:\Users\Administrator>pip install D:\software\TA_Lib-0.4.24-cp310-cp310-win_amd64.whl
ERROR: TA_Lib-0.4.24-cp310-cp310-win_amd64.whl is not a supported wheel on this platform.

(python38) C:\Users\Administrator>conda install Ta-lib
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:

  - ta-lib

Current channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - https://repo.anaconda.com/pkgs/main/win-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/win-64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://repo.anaconda.com/pkgs/msys2/win-64
  - https://repo.anaconda.com/pkgs/msys2/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.



(python38) C:\Users\Administrator>pip install D:\software\TA_Lib-0.4.21-cp38-cp38-win_amd64.whl
Processing d:\software\ta_lib-0.4.21-cp38-cp38-win_amd64.whl
Installing collected packages: TA-Lib
Successfully installed TA-Lib-0.4.21


(python38) C:\Users\Administrator>pip install fake_useragent -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting fake_useragent
  Downloading http://pypi.doubanio.com/packages/8d/f3/28e7af53e09c9218140901dc23fdd75cdaf3e64d1d06f61801cf2a841bcb/fake_useragent-1.1.3-py3-none-any.whl (50 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 50.5/50.5 kB ? eta 0:00:00
Collecting importlib-resources>=5.0
  Downloading http://pypi.doubanio.com/packages/38/71/c13ea695a4393639830bf96baea956538ba7a9d06fcce7cef10bfff20f72/importlib_resources-5.12.0-py3-none-any.whl (36 kB)
Collecting zipp>=3.1.0
  Downloading http://pypi.doubanio.com/packages/5b/fa/c9e82bbe1af6266adf08afb563905eb87cab83fde00a0a08963510621047/zipp-3.15.0-py3-none-any.whl (6.8 kB)
Installing collected packages: zipp, importlib-resources, fake_useragent
Successfully installed fake_useragent-1.1.3 importlib-resources-5.12.0 zipp-3.15.0

(python38) C:\Users\Administrator>pip install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting requests
  Downloading http://pypi.doubanio.com/packages/96/80/034ffeca15c0f4e01b7b9c6ad0fb704b44e190cde4e757edbd60be404c41/requests-2.30.0-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.5/62.5 kB ? eta 0:00:00
Collecting charset-normalizer<4,>=2
  Downloading http://pypi.doubanio.com/packages/26/20/83e1804a62b25891c4e770c94d9fd80233bbb3f2a51c4fadee7a196e5a5b/charset_normalizer-3.1.0-cp38-cp38-win_amd64.whl (96 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.4/96.4 kB 5.4 MB/s eta 0:00:00
Collecting urllib3<3,>=1.21.1
  Downloading http://pypi.doubanio.com/packages/4b/1d/f8383ef593114755429c307449e7717b87044b3bcd5f7860b89b1f759e34/urllib3-2.0.2-py3-none-any.whl (123 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 123.2/123.2 kB ? eta 0:00:00
Collecting idna<4,>=2.5
  Downloading http://pypi.doubanio.com/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB ? eta 0:00:00
Collecting certifi>=2017.4.17
  Downloading http://pypi.doubanio.com/packages/9d/19/59961b522e6757f0c9097e4493fa906031b95b3ebe9360b2c3083561a6b4/certifi-2023.5.7-py3-none-any.whl (156 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 157.0/157.0 kB ? eta 0:00:00
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2023.5.7 charset-normalizer-3.1.0 idna-3.4 requests-2.30.0 urllib3-2.0.2

(python38) C:\Users\Administrator>pip install prettytable -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting prettytable
  Downloading http://pypi.doubanio.com/packages/7a/cd/bec5850e23eb005c6fe30fe4c26bafd9a07b3d2524771f22a0fa01270078/prettytable-3.7.0-py3-none-any.whl (27 kB)
Collecting wcwidth
  Downloading http://pypi.doubanio.com/packages/20/f4/c0584a25144ce20bfcf1aecd041768b8c762c1eb0aa77502a3f0baa83f11/wcwidth-0.2.6-py2.py3-none-any.whl (29 kB)
Installing collected packages: wcwidth, prettytable
Successfully installed prettytable-3.7.0 wcwidth-0.2.6

(python38) C:\Users\Administrator>pip install openpyxl -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting openpyxl
  Downloading http://pypi.doubanio.com/packages/6a/94/a59521de836ef0da54aaf50da6c4da8fb4072fb3053fa71f052fd9399e7a/openpyxl-3.1.2-py2.py3-none-any.whl (249 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 250.0/250.0 kB 15.0 MB/s eta 0:00:00
Collecting et-xmlfile
  Downloading http://pypi.doubanio.com/packages/96/c2/3dd434b0108730014f1b96fd286040dc3bcb70066346f7e01ec2ac95865f/et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.1.2

(python38) C:\Users\Administrator>pip install D:\software\TA_Lib-0.4.24-cp38-cp38-win_amd64.whl
Processing d:\software\ta_lib-0.4.24-cp38-cp38-win_amd64.whl
Requirement already satisfied: numpy in d:\programdata\anaconda3\envs\python38\lib\site-packages (from TA-Lib==0.4.24) (1.24.3)
Installing collected packages: TA-Lib
  Attempting uninstall: TA-Lib
    Found existing installation: TA-Lib 0.4.21
    Uninstalling TA-Lib-0.4.21:
      Successfully uninstalled TA-Lib-0.4.21
Successfully installed TA-Lib-0.4.24

(python38) C:\Users\Administrator>

Guess you like

Origin blog.csdn.net/popboy29/article/details/130957011