Python学习之Pandas学习——jupyter notebook运行环境安装

2023-02-23 19:20:00
pjd
原创 1057
摘要:pandas运行环境创建

###首先确保电脑没有安装anaconda(或单独安装过python)和jupyter notebook。

###如果单独安装过python,可以先删除,如果不删除的话安装anaconda会再次安装python,如果你能够区分也可以。

###如果安装过anaconda和jupyter notebook,需要重新检查电脑python的环境变量。我就是属于当初学习多种模块和框架时,搞了很多的环境,即单独安装过python,又安装过anaconda和jupyter notebook,由于对环境变量没有吃透,这这里踩了几次坑,重新卸载安装和配置环境变量才得以解决。才了解到,每次搞环境,都要注意环境变量的配置。否则明明在新建的环境中安装了pandas模块,在pip list里也能查到这个模块,但是导入模块时,就会出现多个python版本不兼容等问题。从之前的3.9,3.10,3.11最后统一成3.9。


1.安装Anaconda

    下载相应版本的安装包,一路下一步。

     国内镜像地址: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

     官网地址:https://repo.anaconda.com/archive/


2.启动anaconda powershell prompt

    在base下查看现有环境列表:


conda env list

 界面显示:

(base) PS C:\WINDOWS\system32> conda env list
# conda environments:
#
base                         D:\Anaconda3
mydjango                 D:\Anaconda3\envs\mydjango
mypyqt6                   D:\Anaconda3\envs\mypyqt6
mypyqt6                   d:\Anaconda3\envs\mypyqt6


3.创建一个新环境
conda create -n mypandas python=3.9
界面显示,期间需要确认安装包,输入y:
(base) PS C:\WINDOWS\system32> conda create -n mypandas python=3.9
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
  current version: 22.9.0
  latest version: 23.1.0
Please update conda by running
    $ conda update -n base -c defaults conda
## Package Plan ##
  environment location: D:\Anaconda3\envs\mypandas
  added / updated specs:
    - python=3.9
The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    openssl-1.1.1t             |       h2bbff1b_0         5.5 MB
    wheel-0.38.4               |   py39haa95532_0          83 KB
    ------------------------------------------------------------
                                           Total:         5.6 MB
The following NEW packages will be INSTALLED:
  ca-certificates    pkgs/main/win-64::ca-certificates-2023.01.10-haa95532_0 None
  certifi            pkgs/main/win-64::certifi-2022.12.7-py39haa95532_0 None
  openssl            pkgs/main/win-64::openssl-1.1.1t-h2bbff1b_0 None
  pip                pkgs/main/win-64::pip-22.3.1-py39haa95532_0 None
  python             pkgs/main/win-64::python-3.9.16-h6244533_0 None
  setuptools         pkgs/main/win-64::setuptools-65.6.3-py39haa95532_0 None
  sqlite             pkgs/main/win-64::sqlite-3.40.1-h2bbff1b_0 None
  tzdata             pkgs/main/noarch::tzdata-2022g-h04d1e81_0 None
  vc                 pkgs/main/win-64::vc-14.2-h21ff451_1 None
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2 None
  wheel              pkgs/main/win-64::wheel-0.38.4-py39haa95532_0 None
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py39haa95532_2 None
Proceed ([y]/n)? y
Downloading and Extracting Packages
wheel-0.38.4         | 83 KB     | ############################################################################ | 100%
openssl-1.1.1t       | 5.5 MB    | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate mypandas
#
# To deactivate an active environment, use
#
#     $ conda deactivate
Retrieving notices: ...working... done


4.激活运行环境


conda activate mypandas
显示界面:
(base) PS C:\WINDOWS\system32> conda activate mypandas
(mypandas) PS C:\WINDOWS\system32>


5.进入到一个项目目录中


(mypandas) PS C:\WINDOWS\system32> cd D:\webproject\mypandaspro
(mypandas) PS D:\webproject\mypandaspro>

6.在项目目录中创建一个requirements.txt文件,并添加以下内容后保存,注意可以适当更新一下当前最新稳定版本


pandas==1.1.5
Flask==1.1.1
PyMySQL==0.9.3
pyecharts==1.7.1
requests==2.22.0
seaborn==0.10.0
SQLAlchemy==1.3.13
xlrd==1.2.0



7.利用 pip清华源 PyPI 临时镜像下载

运行以下命令:


pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r .\requirements.txt
显示界面:


(mypandas) PS D:\webproject\mypandaspro> pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r .\requirements.txt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pandas==1.1.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/46/f9/d78955c88e045fa220392cc311e41764f8decf868594b5f92d8fa1d1b39e/pandas-1.1.5-cp39-cp39-win_amd64.whl (8.9 MB)
     ---------------------------------------- 8.9/8.9 MB 1.8 MB/s eta 0:00:00
Collecting Flask==1.1.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94 kB)
     ---------------------------------------- 94.5/94.5 kB 2.7 MB/s eta 0:00:00
Successfully installed Flask-1.1.1 Jinja2-3.1.2 MarkupSafe-2.1.2 PyMySQL-0.9.3 SQLAlchemy-1.3.13 Werkzeug-2.2.3 chardet-3.0.4 click-8.1.3 colorama-0.4.6 contourpy-1.0.7 cycler-0.11.0 fonttools-4.38.0 idna-2.8 importlib-resources-5.12.0 itsdangerous-2.1.2 kiwisolver-1.4.4 matplotlib-3.7.0 numpy-1.24.2 packaging-23.0 pandas-1.1.5 pillow-9.4.0 prettytable-3.6.0 pyecharts-1.7.1 pyparsing-3.0.9 python-dateutil-2.8.2 pytz-2022.7.1 requests-2.22.0 scipy-1.10.1 seaborn-0.10.0 simplejson-3.18.3 six-1.16.0 urllib3-1.25.11 wcwidth-0.2.6 zipp-3.14.0
(mypandas) PS D:\webproject\mypandaspro>

 8.启动jupyter notebook

命令行输入启动命令:


jupyter notebook

如果显示没有安装jupyter notebook,请在当前新创建的环境下命令行输入以下命令进行安装 :


pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter notebook


安装后输入启动命令:

jupyter notebook


显示界面,并同时自动打开浏览器相应地址:



(mypandas) PS D:\webproject\mypandaspro> jupyter notebook
[I 20:32:42.076 NotebookApp] Serving notebooks from local directory: D:\webproject\mypandaspro
[I 20:32:42.076 NotebookApp] Jupyter Notebook 6.5.2 is running at:
[I 20:32:42.076 NotebookApp] http://localhost:8888/?token=XXXXXXX
[I 20:32:42.091 NotebookApp]  or http://127.0.0.1:8888/?token=XXXXX
[I 20:32:42.091 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 20:32:42.185 NotebookApp]
    To access the notebook, open this file in a browser:
        file:///C:XXXXX-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=XXXXX
     or http://127.0.0.1:8888/?token=XXXXXXX
0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.01s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
浏览器界面:


9.创建一个文件

在notebook右上角,选择new 创建 “ Python 3 (ipykernel)”,进入一个新文件界面,点击+号,就可以输入代码了,点击Run就可以执行代码了。


JupyterNotebook代码提示与自动补齐

Jupyter Notebook代码提示及补齐
Tab键可补齐代码

代码提示功能需要装插件

1.打开 Anaconda Navigator,点击 Environments -> base (root) -> Open Terminal

2.在命令终端里依次执行如下命令

pip install jupyter_contrib_nbextensions

jupyter contrib nbextension install --user

pip install jupyter_nbextensions_configurator

jupyter nbextensions_configurator enable --user
安装完成后打开Jupyter Notebook。此时可以看到菜单项上(与Files、Running等一行)增加了一个Nbextensions;点击Nbextensions进行配置如图,勾选Hinterland


进行使用测试,重新打开Jupyter Notebook进行使用