Python环境配置
Linux下Python开发环境配置
pyenv
pyenv是Linux环境下的一款python版本控制工具,可以实现在不同路径下实现单独隔离的python环境,在服务器上实现部署多版本python程序。
1.创建用户python并切换
[[email protected] ~]# useradd python
[[email protected] ~]# su - python
[[email protected] ~]$
yum 命令在普通用户下使用需使用sudo权限,需要更改配置文件,默认sudoers文件是没有write权限的,需要先添加write权限后进行编辑,完成后别忘记取消write权限。
[[email protected] ~]# cd /etc
[[email protected] etc]# ll sudoers
-r--r-----. 1 root root 4328 Oct 30 22:38 sudoers
[[email protected] etc]# chmod u+w sudoers
[[email protected] etc]# vim sudoers
[[email protected] etc]# chmod u-w sudoers
在标记位置按照上面一行添加一行,将root改为需要添加sudo权限的用户名
权限添加完成后,在普通用户下再执行相关命令时,在命令前面添加sudo 即可
2.安装git
yum install git -y
3.安装Python编译依赖
yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
4.安装pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
如果出现报错curl: (35) SSL connect error
需配置一个较新包yum源并更新nss
[[email protected] etc]# vim /etc/yum.repos.d/update.repo
添加如下配置信息
[updates]
name=CentOS-Updates
baseurl=https://mirrors.aliyun.com/centos/6.9/os/x86_64
gpgcheck=0
完成后更新nss
yum update nss
5.安装完成后会提示需要配置环境变量
需要在~/.bashrc文件追加下面三行
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
之后通过命令重载该文件或将用户退出重新登录刷新
source ~/.bash_profile
完成后敲击pyenv命令,如有响应则表明已经安装并配置成功。
- 常用命令
pyenv install x.x.x -v 安装对应版本的python
pyenv local x.x.x 设置当前目录版本 新建子目录继承
pyenv global x.x.x 设置当前账户全局版本
pyenv versions system 返回系统默认版本
pyenv virtualenv x.x.x name 虚拟py环境
pyenv activate name 切换成虚拟环境
pyenv versions 查看当前python版本
安装时默认从Python官网下载对应版本,为了方便,可先自行下载安装包,然后将其放在~/.pyenv/cache文件夹内,命令解压后再运行安装命令。
如果使用xftp等远程软件创建cache文件夹的话,可能会出现目录权限为root问题,如果出现需要使用chown和chgrp命令更改属主属组。
Pycharm
可通过xftp将安装包拷贝到python用户家目录下,然后通过tar命令解压之后,进入解压出的文件夹,的bin目录下,可以看到具有执行权限的pycharm.sh,.运行该程序即可。
[[email protected] bin]$ ls
format.sh idea.properties pycharm64.vmoptions restart.py
fsnotifier inspect.sh pycharm.png
fsnotifier64 log.xml pycharm.sh
fsnotifier-arm printenv.py pycharm.vmoptions
[[email protected] bin]$ . pycharm.sh
之后就可以开始创建项目了
选完基础设置即可进入界面,然后默认窗口点击close就可以开始编辑项目了
进入后,右击项目,新建python文件并设置文件名后就可以开始敲代码了,完成后,可以按右上角绿色三角,或快捷键ctrl+shift+F10执行。
jupyter
是ipython的增强版,适合初学者使用。
pip指向aliyun
~/.pip/pip.conf
local
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
安装
pip install jupyter
开启
jupyter notebook --ip=x.x.x.x --port=xxxx
[[email protected] ~]$ jupyter notebook --ip=0.0.0.0 --port=8888
[I 22:34:05.696 NotebookApp] Writing notebook server cookie secret to /run/user/1001/jupyter/notebook_cookie_secret
[I 22:34:06.970 NotebookApp] Serving notebooks from local directory: /home/python
[I 22:34:06.970 NotebookApp] The Jupyter Notebook is running at:
[I 22:34:06.970 NotebookApp] http://(centos7.localdomain or 127.0.0.1):8888/?token=b101cb917f3b9f337b93aee9f4bd83da7d46ec1ee04dde47
[I 22:34:06.970 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 22:34:06.978 NotebookApp]
To access the notebook, open this file in a browser:
file:///run/user/1001/jupyter/nbserver-11299-open.html
Or copy and paste one of these URLs:
http://(centos7.localdomain or 127.0.0.1):8888/?token=b101cb917f3b9f337b93aee9f4bd83da7d46ec1ee04dde47
This tool has been deprecated, use 'gio open' instead.
See 'gio help open' for more info.
开启后可能需要验证**,输入开启开启jupyter后的token提示后面的密码即可,或者将密码追加到打开网址后面重新打开网页。进入后我们就可以使用了。
开启后可能出现命令行提示成功但是无法打开jupyter的情况,可能是防火墙阻止了,此时关闭防火墙并重启一般可以解决。
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
windows下Python开发环境配置
Python
可以到Python官网下载对应的安装包,注意下载和当前CPU架构相符合的版本
https://python.org/
建议标注处打钩,可以免去手工配置环境变量的麻烦。然后一般不用客制化安装,直接一键安装即可。之后我们运行快捷方式即可打开简单交互窗口。
windows环境变量配置
Pycharm
如无需特殊设置,默认设置下一步安装即可。
jupyter
安装可在cmd命令行界面操作
pip install jupyter
重新指向pip源
需在用户家目录(win+R运行.)新建pip文件夹,进入后配置pip.conf,或者在%appdata%/pip文件夹新建pip.conf,编辑添加下面几行即可。
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
运行无需指定ip和端口
jupyter notebook