pyenv&&pipenv环境搭建
更新最新pip
python3 -m pip install --upgrade pip
添加国内源加速
国内源地址:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://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/
创建配置文件
vim ~/.pip/pip.conf
添加如下内容
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host = pypi.mirrors.ustc.edu.cn
pyenv
安装pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
cd ~/.pyenv && src/configure && make -C src
配置相关环境变量
- add the commands to ~/.bashrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
- to add to ~/.profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.profile
- 重启shell更新环境变量
exec "$SHELL"
- 测试pyenv是否安装成功
:~$ pyenv --version
pyenv 2.3.17-4-gb3c91b37
更新pyenv
pyenv update
卸载pyenv
rm -fr ~/.pyenv
删除相关环境变量
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
.profile 和 .bashrc
~/.profile与~/.bashrc都具有个性化定制功能,但~/.profile可以设定本用户专有的路径、环境变量等,它只能登入的时候执行一次。
~/.bashrc也是某用户专有设定文档,可以设定路径、命令别名,每次 shell script 的执行都会使用它一次。
python环境管理
- 安装制定版本python
pyenv install 3.4.1
- 安装依赖库
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
sudo apt install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev -y
- 下载OpenSSL
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz.sha256
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz
-
编译之前检查
sha256sum openssl-1.1.1s.tar.gz
tar xf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s
- 进行编译
./config no-shared # MUST use `no-shared` config, or you will always need LD_LIBRARY_PATH env
make -j32
make test
- 进行安装
cd ~/.pyenv/build/openssl-1.1.1s
CFLAGS=-I$(pwd)/include LDFLAGS=-L$(pwd) ~/.pyenv/bin/pyenv install 3.6.0
- 安装新版本后,刷新
pyenv rehash
- 查看当前版本
pyenv version
- 查看pyenv已经托管了哪些 python 版本
$ pyenv versions
* system (set by /home/book/.pyenv/version)
3.6.0
3.7.0
3.8.6
- 切换版本
pyenv shell <version> -- select just for current shell session
pyenv local <version> -- automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version> -- select globally for your user account
pipenv
官方网站
安装
安装pipenv
pipenv install --user pip pipenv --upgrade
pipenv --version
使用示例
mkdir pipenv_test
cd pipenv_test
pipenv_test$ pipenv --python 3
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /home/book/cmakeCookBook/pipenv_test/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
⠴ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 854ms
creator CPython3Posix(dest=/home/book/.local/share/virtualenvs/pipenv_test--n-V7Fvh, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/book/.local/share/virtualenv)
added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
✔ Successfully created virtual environment!
- 设置LANG环境变量
sudo apt install -y locales
sudo locale-gen en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
- 进入虚拟环境
pipenv shell
- 退出虚拟环境
exit
- 查看虚拟环境管理目录
pipenv --venv
/home/book/.local/share/virtualenvs/pipenv_test--n-V7Fvh
- 查看虚拟环境依赖库
pipenv graph
Flask==2.2.3
- click [required: >=8.0, installed: 8.1.3]
- importlib-metadata [required: >=3.6.0, installed: 6.1.0]
- zipp [required: >=0.5, installed: 3.15.0]
- itsdangerous [required: >=2.0, installed: 2.1.2]
- Jinja2 [required: >=3.0, installed: 3.1.2]
- MarkupSafe [required: >=2.0, installed: 2.1.2]
- Werkzeug [required: >=2.2.2, installed: 2.2.3]
- MarkupSafe [required: >=2.1.1, installed: 2.1.2]
- 安装开发依赖包
pipenv_test$ pipenv install --dev requests
Installing requests...
Resolving requests...
Installing...
Adding requests to Pipfile's [dev-packages] ...
✔ Installation Succeeded
Pipfile.lock (7ec6df) out of date, updating to (9a3421)...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (bf91257fb93de95fce0cd5b2229fc0815cdb7d0ab021f1ab3cb8fdc53c9a3421)!
Installing dependencies from Pipfile.lock (9a3421)...
Installing dependencies from Pipfile.lock (9a3421)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
- 宿主机环境运行虚拟环境命令
pipenv_test$ pipenv run pip list
Package Version
------------------ ---------
certifi 2022.12.7
charset-normalizer 3.1.0
click 8.1.3
Flask 2.2.3
idna 3.4
importlib-metadata 6.1.0
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.2
pip 23.0.1
requests 2.28.2
setuptools 67.4.0
urllib3 1.26.15
Werkzeug 2.2.3
wheel 0.38.4
zipp 3.15.0
- python虚拟环境共享或迁移
mkdir other_team
cp ../pipenv_test/Pipfile .
- 安装工作库和开发库
pipenv install --dev
Creating a virtualenv for this project...
Pipfile: /home/book/cmakeCookBook/other_team/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
⠙ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 111ms
creator CPython3Posix(dest=/home/book/.local/share/virtualenvs/other_team-oftCZxRe, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/book/.local/share/virtualenv)
added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
⠹ Creating virtual environment...✔ Successfully created virtual environment!
Virtualenv location: /home/book/.local/share/virtualenvs/other_team-oftCZxRe
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Locking [dev-packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (bf91257fb93de95fce0cd5b2229fc0815cdb7d0ab021f1ab3cb8fdc53c9a3421)!
Installing dependencies from Pipfile.lock (9a3421)...
Installing dependencies from Pipfile.lock (9a3421)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
- 进入迁移后的虚拟环境
pipenv shell
pip list
Package Version
------------------ ---------
certifi 2022.12.7
charset-normalizer 3.1.0
click 8.1.3
Flask 2.2.3
idna 3.4
importlib-metadata 6.1.0
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.2
pip 23.0.1
requests 2.28.2
setuptools 67.4.0
urllib3 1.26.15
Werkzeug 2.2.3
wheel 0.38.4
zipp 3.15.0
exit
- 删除虚拟环境
pipenv --rm
运行示例代码
cd pipenv_test
pipenv run python main.py
Pipfile添加脚本
tee -a Pipfile << 'EOF'
[scripts]
start = "python main.py"
test = "pytest"
list = "pip list"
- 运行脚本
pipenv rum start
pipenv run list

浙公网安备 33010602011771号