linux环境下使用pyenv ;在windows环境下使用pycharm
**最小化安装linux
pyenv安装使用git
#yum install git -y
python安装依赖
#yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
创建python用户
#useradd python
#passwd python
使用python用户登录
$curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer是一个shell脚本内容如下:
`#!/usr/bin/env bash
set -e
[ -n “$PYENV_DEBUG” ] && set -x
if [ -z “
P
Y
E
N
V
R
O
O
T
"
]
;
t
h
e
n
P
Y
E
N
V
R
O
O
T
=
"
PYENV_ROOT" ]; then PYENV_ROOT="
PYENVROOT"];thenPYENVROOT="{HOME}/.pyenv”
fi
colorize() {
if [ -t 1 ]; then printf “\e[%sm%s\e[m” “$1” “$2”
else echo -n “$2”
fi
}
# Checks for .pyenv
file, and suggests to remove it for installing
if [ -d “KaTeX parse error: Expected '}', got 'EOF' at end of input: …ly remove the '{PYENV_ROOT}’ directory first.”
echo
} >&2
exit 1
fi
shell=“
1
"
i
f
[
−
z
"
1" if [ -z "
1"if[−z"shell” ]; then
shell=“
(
p
s
c
−
p
"
(ps c -p "
(psc−p"PPID” -o ‘ucomm=’ 2>/dev/null || true)"
shell="KaTeX parse error: Expected '}', got '#' at position 7: {shell#̲#-}" shell="{shell%% *}"
shell="
(
b
a
s
e
n
a
m
e
"
(basename "
(basename"{shell:-$SHELL}")"
fi
failed_checkout() {
echo “Failed to git clone $1”
exit -1
}
checkout() {
[ -d “$2” ] || git clone --depth 1 “$1” “$2” || failed_checkout “$1”
}
if ! command -v git 1>/dev/null 2>&1; then
echo “pyenv: Git is not installed, can’t continue.” >&2
exit 1
fi
if [ -n “${USE_GIT_URI}” ]; then
GITHUB=“git://github.com”
else
GITHUB=“https://github.com”
fi
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv.git" "
GITHUB/pyenv/pyenv.git""{PYENV_ROOT}”
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
−
d
o
c
t
o
r
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv-doctor.git" "
GITHUB/pyenv/pyenv−doctor.git""{PYENV_ROOT}/plugins/pyenv-doctor”
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
−
i
n
s
t
a
l
l
e
r
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv-installer.git" "
GITHUB/pyenv/pyenv−installer.git""{PYENV_ROOT}/plugins/pyenv-installer”
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
−
u
p
d
a
t
e
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv-update.git" "
GITHUB/pyenv/pyenv−update.git""{PYENV_ROOT}/plugins/pyenv-update”
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
−
v
i
r
t
u
a
l
e
n
v
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv-virtualenv.git" "
GITHUB/pyenv/pyenv−virtualenv.git""{PYENV_ROOT}/plugins/pyenv-virtualenv”
checkout “
G
I
T
H
U
B
/
p
y
e
n
v
/
p
y
e
n
v
−
w
h
i
c
h
−
e
x
t
.
g
i
t
"
"
{GITHUB}/pyenv/pyenv-which-ext.git" "
GITHUB/pyenv/pyenv−which−ext.git""{PYENV_ROOT}/plugins/pyenv-which-ext”
if ! command -v pyenv 1>/dev/null; then
{ echo
colorize 1 “WARNING”
echo “: seems you still have not added ‘pyenv’ to the load path.”
echo
} >&2
case “$shell” in
bash )
profile="~/.bashrc"
;;
zsh )
profile="~/.zshrc"
;;
ksh )
profile="~/.profile"
;;
fish )
profile="~/.config/fish/config.fish"
;;
* )
profile=“your profile”
;;
esac
{ echo “# Load pyenv automatically by adding”
echo “# the following to
p
r
o
f
i
l
e
:
"
e
c
h
o
c
a
s
e
"
{profile}:" echo case "
profile:"echocase"shell” in
fish )
echo “set -x PATH “${PYENV_ROOT}/bin” $PATH”
echo ‘status --is-interactive; and . (pyenv init -|psub)’
echo ‘status --is-interactive; and . (pyenv virtualenv-init -|psub)’
;;
* )
echo “export PATH=”${PYENV_ROOT}/bin:$PATH""
echo “eval “$(pyenv init -)””
echo “eval “$(pyenv virtualenv-init -)””
;;
esac
} >&2
fi`
以上脚本有时会报错,需手动配以下配置
$git clone https://github.com/pyenv/pyenv.git ~/.pyenv
**在python用户的~/.bash_profile中追加
$ vi .bash_profile
export PATH="/home/python/.pyenv/bin:$PATH"
eval “$(pyenv init -)”
eval “$(pyenv virtualenv-init -)”**
$source ~/.bash_profile
$
$pyenv help install ;pyenv的帮助
$pyenv install -l ;列出能安装的python版本
$pyenv install -v 3.5.3 ;安装3.5.3的版本
$pyenv install -v 3.6.1 ;安装3.6.1的版本
$pyenv update ;更新pyenv
如果不能上网或网速过慢,可以在.pyenv下手动建立cache文件夹,将下载好的文件cp到其中,最好装.gz.xz.tgz三种包都拷入,不确定会调用哪个文件;$mkdir cache
p
y
e
n
v
v
e
r
s
i
o
n
s
;
列
出
已
安
装
版
本
[
p
y
t
h
o
n
@
b
o
g
o
n
]
pyenv versions ;列出已安装版本 [python@bogon ~]
pyenvversions ;列出已安装版本[python@bogon ] pyenv versions
- system (set by /home/python/.pyenv/version)
3.5.3
3.6.1
$pyenv version ;现使用python版本
Virtualenv插件,在plugins/pyenv-virrualenv下
$pyenv virtualenv 3.6.1 seaship361 ;创建一个3.6.1版本的独立空间
真实目录在.pyenv/versions/
Python虚拟环境创建
接下来安装virtualenv
Check out pyenv-virtualenv into plugin directory
$ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
正常来说,pyenv-virtualenv插件是安装pyenv自带的,在pyenv的plugin目录里可以看到它
[wls@xxxxxxxx ~]$ ll ~/.pyenv/plugins/
total 24
drwxrwxr-x 4 wls wls 4096 Dec 2 19:43 pyenv-doctor
drwxrwxr-x 5 wls wls 4096 Dec 2 19:43 pyenv-installer
drwxrwxr-x 5 wls wls 4096 Dec 2 19:43 pyenv-update
drwxrwxr-x 8 wls wls 4096 Dec 2 19:44 pyenv-virtualenv
drwxrwxr-x 4 wls wls 4096 Dec 2 19:44 pyenv-which-ext
drwxrwxr-x 5 wls wls 4096 Dec 2 19:43 python-build
如果你的目录里没有,从github拉一份下来就好,项目地址:https://github.com/pyenv/pyenv-virtualenv,安装步骤README有写
$ # 虚拟环境后面的命名可以随便改,改成自己喜欢的即可
$ pyenv virtualenv 3.7.3 pyweb373
创建完虚拟环境后查看是否创建成功
$ pyenv versions
- system (set by /home/lucas/.pyenv/version)
3.7.3
3.7.3/envs/pyweb373
pyweb373
然后就可以进入项目文件夹中,把Python切换到虚拟环境中
$ cd ~/project1
$ pyenv local pyweb373
这样,你就成功地将这个项目的环境换成虚拟环境,可以愉快地安装第三方库、撸代码了。
在希望完成的目录中执行:
$pyenv local 3.6.1(system为默认) ;使用公用的3.6.1的环境,不同的人会互相干扰
$pyenv virtualenv seaship361 ;这个实用建立自己独立的语言环境。
其实质就是在~/.bash_profile中加了内容实现的
pip通用配置,设为国内镜像,速度快
$mkdir ~/.pip
配置文件在~/.pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
在不财的虚拟环境中,安装redis包,使用pip list看看效果。
$pip -V
安装ipython
$pip install ipython
$ipython
安装Jupyter,也会安装ipython的
$pip install jupyter
$jupyter notebook help
$jupyter notebook --ip=0.0.0.0 --no-browser
—————————————————————————————————————————————
https://github.com/pyenv/pyenv
安装之前,确保安装了git
#yum install git -y
安装其它依赖
#yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel
创建用户
#useradd python
使用python用户登录
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv" ’ >> ~/.bash_profile
$ echo ‘export PATH=“
P
Y
E
N
V
R
O
O
T
/
b
i
n
:
PYENV_ROOT/bin:
PYENVROOT/bin:PATH” ’ >> ~/.bash_profile
$ echo -e ’ if command -v pyenv 1>/dev/null 2>1; then\n eval “$(pyenv init -)”\nfi’ >> ~/.bash_profile
$ exec “$SHELL”
$pyenv install 2.7.8
离线安装
分别去官网下载pyenv 和virtualenv插件
pyenv https://github.com/pyenv/pyenv
virtualenv 插件 https://github.com/pyenv/pyenv-virtualenv