【Python小技巧】通过Anaconda虚拟环境解决Python新版本无法运行指定脚本的问题,并实现Notepad++一键运行 Python Anaconda虚拟环境

文章目录

  • 前言
  • 一、创建虚拟环境
  • 二、Nodepad++配置快捷键运行Python虚拟环境
  • 总结
  • 附录


前言

笔者今天碰到一个奇怪的问题,Python从3.8升级到3.10 后,一直以来还不错。但今天运行某个函数总是没反应就结束了。刚开始以为是pandas1.5.3升级到2.0.0版本的问题,当时因为2.0pandas运行很快就升级了,先排除这个。把pandas先升级到最新版,不行。后又降到1.5.3版本还是不行。一样的代码换到另外一台Python3.8.16的电脑就可以,把这个pandas升级到最新版也OK。

突然想到应该可能是Python版本的问题

Anaconda支持虚拟环境,就是来解决这种尴尬的,于是我就想到创建一个低版本的Python环境。下面就来试试吧,不过装完环境还有很多库需要单独安装,在后面附录里。

一、创建虚拟环境

Anaconda 非常方便创建和管理虚拟环境,如果需要多个python版本,我们就可以使用conda命令行创建不同的虚拟环境的。具体步骤如下:

  1. 创建之前,我们先看看当前都有哪些虚拟环境,
conda info --envs

一般Anaconda 安装好,默认有一个base的环境。结果如下:

(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,即可查看当前的虚拟环境中都包含哪些package
输入: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 删除指定名称虚拟环境

二、Nodepad++配置快捷键运行Python虚拟环境

  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库时还是会报错,而且以前的wheel宝库:https://www.lfd.uci.edu/~gohlke/pythonlibs 也不能下载了,还好找到了库存货。终于搞定。

这里不得不说,紧跟潮流没错,但Python每个版本的稳定运行是需要匹配的。大家如果做测试无所谓,作为作业环境还是谨慎一些!

附录


(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>

你可能感兴趣的:(Python经验池,python,notepad++,开发语言)