Windows环境下的Python环境搭建

1、Windows环境下pyenv-win的安装

pyenv用于Linux系统中,用来管理多版本的Python环境,由bash脚本编写,使用pyenv-installer来安装。pyenv github下载地址:https://github.com/pyenv/pyenv。
pyenv-win用于Windows环境中,下载地址:https://github.com/pyenv-win/pyenv-win。
Windows 10环境中pyenv-win的安装步骤如下(从下载的压缩包安装):
(1)解压pyenv-win压缩包pyenv-win-master.zip在E:\Program Files (x86)目录下。
(2)配置环境变量PATH,添加如下条目:
E:\Program Files (x86)\pyenv-win-master\pyenv-win\bin
E:\Program Files (x86)\pyenv-win-master\pyenv-win\shims
(3)在命令行输入:pyenv --version,出现如下输出即可显示pyenv-win已安装成功:pyenv 1.2.2

2、安装Python3.7.3

(1)Python3.7.3是当前最新的稳定版本,下载地址:https://www.python.org/downloads/release/python-373/
(2)Python3.7.3的安装步骤:
1)安装python-3.7.3-amd64.exe,点击“Next”,直到结束。
2)在安装过程中注意点击“Add Python 3.7 to PATH”。
3)安装完成在命令行输入:python --version,得到如下输出:Python 3.7.3,表明Python3.7.3安装成功。

3、pyenv的使用

(1)查看pyenv可以使用的命令

C:\Users\DELL>pyenv help
pyenv 1.2.2
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   exec        Runs an executable by first preparing PATH so that the selected Python

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv-win/pyenv-win#readme

(2)查看pyenv可以使用的python版本,在Linux版本的pyenv中,这些python版本都存放在~/.pyenv/versions/目录下。

C:\Users\DELL>pyenv versions
C:\Users\DELL>pyenv version

(3)在pyenv中安装python解释器

C:\Users\DELL>pyenv install 3.7.3
C:\Users\DELL>pyenv install --list

(4)更新pyenv

C:\Users\DELL>pyenv update

(5)local命令
local命令用来管理Python版本,切换当前目录及其子目录的Python版本, 可以通过删除.python-version文件恢复默认Python版本。
(6)global命令
global命令用来管理Python版本,切换全局默认Python版本。
(7)virtualenv命令
virtualenv用来管理Python虚拟环境,每个Python虚拟环境相当于是一个独立的Python版本。
创建虚拟环境命令: pyenv virtualenv 3.7.3 yanyan
其中,3.7.3是Python的基础版本,yanyan是virtualenv的名字。在Linux版本的pyenv中,yanyan存放在~ /.pyenv/versions/yanyan中,这是一个链接文件,链接的是~/.pyenv/versions/3.7.3/envs/yanyan目录。
(8)uninstall命令
卸载某个python版本的解释器, 包括虚拟环境:pyenv uninstall yanyan

4、pip工具

(1)查看pip工具

C:\Users\DELL>pip help

(2)查看pip版本

pip -V

(3)用pip工具安装Python插件

pip install xxx yyy

例如:用pip同时安装redis和ipython:pip install redis ipython
(4)列出用pip工具已安装的Python插件及其版本

pip list

(5)用pip工具搜索Python插件

pip search KEYWORD
或者:
pypi search KEYWORD

(6)查询pip安装选项

pip help install

(7)用pip导出当前已安装Python插件的名字和版本号,形成requirement文件(该名字可随意)

pip freeze > requirement

按照已导出的一台机器上的Python插件的名字和版本号的requirement文件,用pip工具在另一台机器上安装对应版本的Python插件,目的是可以批量安装Python插件,并保持这些插件之间兼容性与之前环境稳定的机器相同。
安装命令:

pip install -r requirement

(8)pip的通用配置
Linux中:mkdir ~/.pip$ vim ~/.pip/pip.conf,pip.conf文件如下:

[global]
index-url=http://mirrors.aliyun.com/pypi/simple
trusted-host=mirrors.aliyun.com

Windows中:在C:\Users\yanyan,也就是当前Windows登录用户的家目录中(该家目录可以在开始菜单->运行中输入.即可到达),新建pip文件夹,在pip文件夹中新建pip.ini文件,即新建~/pip/pip.ini文件,文件内容如下:

[global]
index-url=http://mirrors.aliyun.com/pypi/simple
trusted-host=mirrors.aliyun.com

即可用阿里云的镜像站点下载 https://pypi.org/ 仓库中的文件,pip也正是pypi(Python Package Index)的简写形式。同样,清华、豆瓣的国内镜像也可以下载pypi的Python包文件。

5、ipython和jupyter

(1)ipython
增强的Python Shell,可以自动补全、自动缩进、支持shell,增加了很多函数。
(2)jupyter
jupyter是从iPython中独立出来的项目,可在浏览器中交互式使用的工具,后台使用ipython实现。快捷键:shift + Enter、Ctrl + Enter、dd、m。
(3)用pip安装ipython
用pip安装ipython命令:C:\Users\DELL>pip install ipython
安装完成出现如下提示:

You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

升级pip:python -m pip install --upgrade pip
然后设置pip从国内的镜像下载python文件。
(4)用pip安装jupyter
安装命令:pip install jupyter
(5)jupyter的使用
1)在浏览器端启动jupyter:jupyter notebook,该jupyter进程监听在本地8888端口。浏览器地址显示为:http://localhost:8888/tree#notebooks, 选择Python 3,进入编辑界面:http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=python3。
2)安装jupyter的服务器端不启动浏览器,要在客户端的浏览器上访问jupyter,则服务器端jupyter的启动命令为:jupyter notebook --ip=0.0.0.0 --no-browser,假设安装jupyter的服务器的ip地址为192.168.2.1,则客户端本地浏览器访问服务器端的jupyter地址如下:http://192.168.2.1:8888/tree。
3)jupyter notebook --ip=192.168.2.1 --port=8888:指定只能由192.168.2.1的客户端访问服务器端的jupyter。
4)给jupyter设置密码:jupyter notebook password

发布了219 篇原创文章 · 获赞 603 · 访问量 129万+

猜你喜欢

转载自blog.csdn.net/gongxifacai_believe/article/details/92161861