python3.6下载地址:https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
linux下python环境配置
统一目录:
- 源码存放位置 $HOME/tools
- 安装目录: /application
安装python环境:
1. 下载
# 创建下载目录
mkdir $HOME/tools
# 切换到tools统一管理源码目录
cd $HOME/tools
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
2. 解压
tar -xf Python-3.6.4.tgz
3. 进入源码包
cd $HOME/tools/Python-3.6.4
4. 配置,编译,安装
./configure --enable-optimizations --prefix=/application/python-3.6.4
# --enable-optimizations 稳定性优化参数
# --prefix 指定安装路径
# 编译和安装
make && make install
5. 创建软链接
ln -s /application/python-3.6.4 /application/python
6. 把python加入环境变量
echo 'export PATH="/application/python/bin:$PATH"' >> /etc/profile
# 注意多个软件加入环境变量中间以 : 隔开
7. 启动python解释器
. /etc/profile
python3
# linux系统中默认为python命令为启动python2解释器,python3启动对应的python3.x版本解释器
# 至此,python环境安装完成
centos下如何给该3.6.4版本安装第三方库?
pip3
# 查看已经安装的第三方库
pip3 list
# pip3 install django python3安装Django程序,可以通过 == 指定软件包版本
# pip3 和 pip 本质是一样的,只是这个pip3指定给安装的python3版本安装软件包
有时我们安装Django时候,下载太慢,可不可以利用国内网速呢?
么问题,通过阿里云源安装
pip3 install django -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
# 不知看官们是否发现一个问题,就是每次安装软件包时候都要 -i 敲大量的重复参数,累不?
# 不累不累 ^_^
如何一本万利解决掉楼上这个问题呢?
1. 在家目录下创建一个 .pip 文件夹
mkdir $HOME/.pip -P
2. 然后在这个目录下创建 pip.conf 文件,并添加点内容
echo -e "[global]\ntrusted-host=mirrors.aliyun.com\nindex-url=http://mirrors.aliyun.com/pypi/simple/" > $HOME/.pip/pip.conf
以上是linux平台下的操作秀,window小伙伴不要羡慕了,献上一计
直接使用pycharm不就得了,解放时间解放智商,何乐而不为?
# 我可生气了 <^>
1. windows下创建%APPDATA%\pip\pip.ini文件来配置
# %HOMEPATH% 是什么东东?
在资源管理器中,不是有个文件路径输入框么,直接输入 %APPDATA%,然后呢,会跳转到一个目录,然后在这个目录下创建一个pip文件夹,进入这个pip文件夹呢,再创建一个pip.ini文件,写入
[global] trusted-host=mirrors.aliyun.com index-url=https://mirrors.aliyun.com/pypi/simple/
完事了,再试试pip安装软件,快不快?
# ?????????? ^_^ ……
centos如何对python安装虚拟化环境?
1. 安装虚拟化库
pip3 install virtualenvwrapper
2. 创建虚拟环境管理目录.pyenv
mkdir $HOME/.pyenv
# $HOME 表示当前用户家目录
3. 在 $HOME/.bashrc添加内容
cat >>$HOME/.bashrc<<beimenchuixue # 使用Distribute和pip方式替代以前setuptools和easy_install export VIRTUALENV_USE_DISTRIBUTE=1 # 指定虚拟环境路径 export WORKON_HOME=$HOME/.pyenv # 指定python虚拟根基 export VIRTUALENVWRAPPER_PYTHON=/application/python/bin/python3 # 指定 vituralenvwrapper虚拟 shell环境 if [ -e /application/python/bin/virtualenvwrapper.sh ];then source /application/python/bin/virtualenvwrapper.sh fi # 虚拟环境pip安装根基 export PIP_VIRTUALENV_BASE=$WORKON_HOME # pip是否遵守虚拟环境 export PIP_RESPECT_VIRTUALENV=true <<beimenchuixue
4. 让改动生效
. $HOME/.bashrc
5. 获得帮助信息
virtualenvwrapper –help # 如果出现帮助信息,至此centos上虚拟环境安装完成
这个虚拟化包安装的常用命名
1. mkvirtualenv [环境名] # 创建基本虚拟环境
2. --python= # 虚拟环境指定python版本
3. rmvirtualenv [环境名] # 删除虚拟环境
4. workon [环境名]
5. deactivate # 退出虚拟环境
6. workon # 列出虚拟环境
以上命令难记又难理解,一般我是这样设置,易读并容易理解写入别名
mkvirtualenv --> mkenv
rmvirtualenv --> rmenv
deactivate --> outenv
sed -i "9i # only want to easy use and read\n\ # you also can set what you like\n\ alias mkenv='mkvirtualenv'\n\ alias rmenv='rmvirtualenv'\n\ alias outenv='deactivate'" $HOME/.bashrc # 这些配置谁执行谁生效,对应当前登录用户
1. 让别名生效
. $HOME/.bashrc
2. 测试
mkenv test # 创建虚拟环境test,并直接进入 outenv # 退出虚拟环境 workon # 检查 rmenv test # 删除test虚拟环境 workon # 检查
# 看官又想知道window下如何创建虚拟化环境,嘻嘻,建议直接使用pycharm,创建项目的是既可以自动创建虚拟环境,可以节省时间,又可以节省少量的智商
唉,上面的步骤我看不懂啊,别急,下面有一键从0开始安装的脚本,直接回车按enter键执行就可以了,一键安装,剩下时间还可以逛逛b站看看动画,岂不美哉,美滋滋
https://github.com/beimenchuixue/shells/blob/master/python3.6_env.sh
#!/bin/sh # author: beimenchuixue # email: 422083556@qq.com # blog: http://www.cnblogs.com/2bjiujiu/ downloadPath="$HOME/tools" installPath="/application" pyBinPath="python/bin" pyVersion="3.6.4" pyPkgName="Python-${pyVersion}" pyDownloadUrl="https://www.python.org/ftp/python/${pyVersion}/${pyPkgName}.tgz" pyRelayPkg="zlib-devel gcc gcc-c++ openssl-devel sqlite-devel wget" pyVirtualPkg="virtualenvwrapper" globalPath="/etc/profile" vitrualEnvPath="$HOME/.pyenv" userBashConf="$HOME/.bashrc" . /etc/init.d/functions function sureOK { [ $1 -eq 0 ] && { action "$2 is" /bin/true } || { action "$2 is" /bin/false exit $1 } } [ -d $downloadPath ] || { mkdir -p $downloadPath sureOK $? "init downloadPath" } function pyRelayPkgInstall { echo "yum install python ...ing" yum install $pyRelayPkg &> /dev/null sureOK $? "pyRelayPkgInstall" } #pass pyRelayPkgInstall function downloadPyPkg { cd $downloadPath echo "dowbload python ...ing" wget -q $pyDownloadUrl sureOK $? "downloadPyPkg" } #pass downloadPyPkg function untarPyPkg { cd $downloadPath tar -xf ${pyPkgName}.tgz sureOK $? "untarPyPkg" } #pass untarPyPkg function pyConfigure { cd $downloadPath/$pyPkgName ./configure --enable-optimizations --prefix=$installPath/$pyPkgName --with-ssl &> /dev/null sureOK $? "pyConfigure" } #pass pyConfigure function pyMakeAndMakeInstall { cd $downloadPath/$pyPkgName echo "make python ...ing, please wait ..ing" make &> /dev/null sureOK $? "pyMake" echo "make install python ...ing, please wait ..ing" make install &> /dev/null sureOK $? "pyMakeInstall" } #pass pyMakeAndMakeInstall function pySoftLink { ln -s $installPath/$pyPkgName $installPath/python sureOK $? "pySoftLink" } #pass pySoftLink function pyGlobalEnv { exportLineNum=`sed -n '/export PATH=/=' $globalPath` [ -z "$exportLineNum" ] && { echo "export PATH=\"$installPath/$pyBinPath:\$PATH\"" >> $globalPath sureOK $? "pyGlobalEnv" } || { middlePath= $(echo `sed -n '/export PATH=/p' $globalPath`| awk -F '[ "]' -v v=$installPath/$pyBinPath '{print $1,$2"\""$2$3":"v"\""}') echo $middlePath sed -i "#exportLineNum s/.*/$middlePath/g" $globalPath sureOK $? "pyGlobalEnv" } } #pass pyGlobalEnv function AliyunPipConf { [ -d $HOME/.pip ] || { mkdir $HOME/.pip -p sureOK $? "init .pip dir" } cd $HOME/.pip echo -e "[global]\ntrusted-host=mirrors.aliyun.com\nindex-url=http://mirrors.aliyun.com/pypi/simple/" > pip.conf sureOK $? "AliyunPipConf" } #pass AliyunPipConf function pyVitrualenvInstall { . $globalPath pip3 install $pyVirtualPkg &> /dev/null sureOK $? "pyVitrualenvInstall" } #pass pyVitrualenvInstall function createPyVitrualenv { [ -d $vitrualEnvPath ] || { mkdir -p $vitrualEnvPath sureOK $? "init vitrualEnvPath" } cat >>$userBashConf<<EOF export VIRTUALENV_USE_DISTRIBUTE=1 export WORKON_HOME=$vitrualEnvPath export VIRTUALENVWRAPPER_PYTHON=$installPath/$pyBinPath . $installPath/$pyBinPath/virtualenvwrapper.sh export PIP_VIRTUALENV_BASE=\$WORKON_HOME export PIP_RESPECT_VIRTUALENV=true EOF sureOK $? "createPyVitrualenv" } #pass createPyVitrualenv function persionPyVirtualCmdAlias { sed -i "9i alias mkenv='mkvirtualenv'\nalias rmenv='rmvirtualenv'\nalias outenv='deactivate'" $userBashConf sureOK $? "persionPyVirtualCmdAlias" } #pass persionPyVirtualCmdAlias main_BeiMenChuiXue() { pyRelayPkgInstall downloadPyPkg untarPyPkg pyConfigure pyMakeAndMakeInstall pySoftLink pyGlobalEnv AliyunPipConf pyVitrualenvInstall persionPyVirtualCmdAlias } main_BeiMenChuiXue # ^_^