困扰我很多天的Python环境问题终于得到解决了,今天把遇到的问题做个复盘

Python库安装很慢

这个问题其实很常见,当我们只是安装了Python3.X的基础运行环境,所拉取库镜像是在国外的,受网速的影响,下一些大一点的可以就很慢,常常下载失败。

常遇到:

1
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/numpy/

第一个解决办法:

1
pip install  xxx  --index-url http://pypi.douban.com/simple/    --trusted-host pypi.douban.com

这是临时的一种方法,可以解燃眉之急

第二种解决办法:

在本地机器新建配置文件,加入国内镜像网站

要在用户的目录下新建个pip文件夹,在里面新建个pip.ini,文件内容配置如下:

1
2
3
4
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com

这样就可以把镜像拉取地址改到国内了,国内其他的镜像网站如下:

1
2
3
4
5
6
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/

阿里云的有点慢,个人感觉豆瓣和清华的很快!

Python手动安装库

大多数库修改了镜像拉去地址都可以安装成功的,而且速度也很快,但是也有很多库依旧无法安装,比如mysqlclient这种,这时候就需要手动安装了,先去网上找到whl文件,下载下来安装即可

有两个下载网站:

pypi.orgwww.lfd.uci.edu

第一个网站并不需要机型版本的选择,直接选择库版本下载即可

第二个就需要了,所以需要先查看自己机器适合库的版本

1
2
# 查看版本
pip debug --verbose

通过第二个网站进行查找,找到合适版本即可,这里以安装mysqlclient为例

下载下来,直接在某个文件夹安装即可,因为python都是加入环境变量了的

1
pip install XXX.whl

当然,也有很大不是whl格式的而是压缩包格式的,方法是一样的,只是安装命令不一样,这里以snowNLP安装为例(snowNLP直接用pip install安装也是可以的)

本地解压,进入解压文件夹,命令行安装即可:

1
python setup.py install

Python环境和Anaconda3

Anaconda3自带了绝大部分Python常用第三方库,非常方便,而Python在刚安装时是没有第三方库的,下载会遇到网络的问题,所以很多时候都会选择Anaconda3,使用pip install 是用的Python的pip工具,用conda install是用的Anaconda3的pip工具,有时候有些第三方库并不支持Anaconda3,就需要使用Python去安装,比如哈工大的分词器pyltp,我在配置系统环境变量的时候,把Anaconda3的配置顺序弄到Python的前面去了,导致每次我pip intsall都是用的Anaconda3去安装。

如何切换成python的呢?很简单,把Python在Path中的变量配置提到Anaconda3之前就行了。

其实这样才是最正确的选择,pip用python的,而Anaconda3有自己的conda,就不会安装错了

虚拟环境

pipenv是一个python包管理工具,它能同时管理python虚拟环境和python依赖,官方推荐。

还有virtuanenv,但是比较老了。可以说pipenv=pip + virtualenv,下面介绍pipenv搭建虚拟环境,实现每个项目一个环境。

下面我新建个文件夹,重新搭建个虚拟环境,整合进Pycharm做环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Microsoft Windows [版本 10.0.18363.1556]
(c) 2019 Microsoft Corporation。保留所有权利。
# pip安装pipenv
D:\>pip install pipenv
Looking in indexes: http://pypi.douban.com/simple
Requirement already satisfied: pipenv in d:\python3.6\lib\site-packages (2022.1.8)
Requirement already satisfied: virtualenv-clone>=0.2.5 in d:\python3.6\lib\site-packages (from pipenv) (0.5.7)
Requirement already satisfied: setuptools>=36.2.1 in d:\python3.6\lib\site-packages (from pipenv) (59.6.0)
Requirement already satisfied: certifi in d:\python3.6\lib\site-packages (from pipenv) (2021.10.8)
Requirement already satisfied: pip>=18.0 in d:\python3.6\lib\site-packages (from pipenv) (21.3.1)
Requirement already satisfied: virtualenv in d:\python3.6\lib\site-packages (from pipenv) (20.13.3)
Requirement already satisfied: importlib-resources>=1.0 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (5.4.0)
Requirement already satisfied: six<2,>=1.9.0 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (1.11.0)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (4.8.3)
Requirement already satisfied: platformdirs<3,>=2 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (2.4.0)
Requirement already satisfied: distlib<1,>=0.3.1 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (0.3.4)
Requirement already satisfied: filelock<4,>=3.2 in d:\python3.6\lib\site-packages (from virtualenv->pipenv) (3.4.1)
Requirement already satisfied: zipp>=0.5 in d:\python3.6\lib\site-packages (from importlib-metadata>=0.12->virtualenv->pipenv) (3.6.0)
Requirement already satisfied: typing-extensions>=3.6.4 in d:\python3.6\lib\site-packages (from importlib-metadata>=0.12->virtualenv->pipenv) (4.1.1)

D:\>pip show pipenv
Name: pipenv
Version: 2022.1.8
Summary: Python Development Workflow for Humans.
Home-page: https://github.com/pypa/pipenv
Author: Pipenv maintainer team
Author-email: distutils-sig@python.org
License: MIT
Location: d:\python3.6\lib\site-packages
Requires: certifi, pip, setuptools, virtualenv, virtualenv-clone
Required-by:

D:\>cd Environment

D:\Environment>cd data-envs
# 在该文件夹中创建pipenv配置文件,以后要配置该虚拟环境就直接进入这个环境用pipenv命令即可
D:\Environment\data-envs>pipenv install
Creating a virtualenv for this project...
Pipfile: D:\Environment\data-envs\Pipfile
Using D:/python3.6/python.exe (3.6.2) to create virtualenv...
[ ==] Creating virtual environment...created virtual environment CPython3.6.2.final.0-64 in 2892ms
creator CPython3Windows(dest=C:\Users\Luffy\.virtualenvs\data-envs-EwQ4Zbmy, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Luffy\AppData\Local\pypa\virtualenv)
added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

Successfully created virtual environment!
Virtualenv location: C:\Users\Luffy\.virtualenvs\data-envs-EwQ4Zbmy
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (ca72e7)!
Installing dependencies from Pipfile.lock (ca72e7)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

D:\Environment\data-envs>dir
驱动器 D 中的卷是 Program
卷的序列号是 654B-777A

D:\Environment\data-envs 的目录

2022/03/16 15:33 <DIR> .
2022/03/16 15:33 <DIR> ..
2022/03/16 15:33 138 Pipfile
2022/03/16 15:33 453 Pipfile.lock
2 个文件 591 字节
2 个目录 284,097,318,912 可用字节
# windows 没有vi vim,所以直接进文件夹用记事本打开查看即可

# 查看虚拟环境位置
D:\Environment\data-envs>pipenv --venv
C:\Users\Luffy\.virtualenvs\data-envs-EwQ4Zbmy
# 查看安装的库
D:\Environment\data-envs>pipenv graph
# 虚拟环境中安装库
D:\Environment\data-envs>pipenv install pandas numpy
Installing pandas...
Adding pandas to Pipfile's [packages]...
Installation Succeeded
Installing numpy...
Adding numpy to Pipfile's [packages]...
Installation Succeeded
Pipfile.lock (ca72e7) out of date, updating to (e86322)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Locking...Building requirements...
Resolving dependencies...
Success!
Updated Pipfile.lock (e86322)!
Installing dependencies from Pipfile.lock (e86322)...
================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

D:\Environment\data-envs>pipenv graph
pandas==1.1.5
- numpy [required: >=1.15.4, installed: 1.19.5]
- python-dateutil [required: >=2.7.3, installed: 2.8.2]
- six [required: >=1.5, installed: 1.16.0]
- pytz [required: >=2017.2, installed: 2021.3]
# 激活环境,进入环境,环境中可以直接运行python程序
D:\Environment\data-envs>pipenv shell
Launching subshell in virtual environment...
Microsoft Windows [版本 10.0.18363.1556]
(c) 2019 Microsoft Corporation。保留所有权利。

(data-envs-EwQ4Zbmy) D:\Environment\data-envs>exit

D:\Environment\data-envs>pipenv --venv
C:\Users\Luffy\.virtualenvs\data-envs-EwQ4Zbmy

D:\Environment\data-envs>

C:\Users\Luffy.virtualenvs\data-envs-EwQ4Zbmy 是虚拟环境命令,我们要整合进Pycharm只需要

虚拟环境安装配置运行streamlit

streamlit可以快速搭建web服务器,在做机器学习可视化的时候非常方便,但是streamlit的安装非常难搞,本人搞了两天才搞好。

之前是在自己台式机搭建好了环境,streamlit这些都弄好了,但是要来图书馆,所以要在自己笔记本也搭建下环境,要学习Django+DRF,要整合虚拟环境,干脆在虚拟环境中安装streamlit

安装配置虚拟环境,前面已经说了,现在重点是安装streamlit,运行streamlit做的应用

streamlit非常不好装,用在线拉去镜像每次都失败,只能通过离线,加载whl的形式进行安装。安装好后,把虚拟环境整合进Pycharm,但是我发现用

1
2
streamlit run app.py

报错了,但是在安装成功后调用实例是可以的,所以我发现应该是运行地方的错误,琢磨了以后,进入虚拟环境,用命令启动应用即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D:\Environment\envs>pipenv shell
Launching subshell in virtual environment...
Microsoft Windows [版本 10.0.18363.1556]
(c) 2019 Microsoft Corporation。保留所有权利。

(envs-f-7jKLmf) D:\Environment\envs>streamlit run D:/pycharm-code-2022/Test/app.py

You can now view your Streamlit app in your browser.

Local URL: http://localhost:8501
Network URL: http://10.176.178.83:8501

Stopping...

(envs-f-7jKLmf) D:\Environment\envs>

运行的文件需要用绝对路径,到这就搭建好虚拟环境去运行streamlit应用了