码迷,mamicode.com
首页 > 编程语言 > 详细

python 设置环境变量和虚拟机环境以及pip 加速

时间:2020-06-29 15:45:09      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:aliyun   默认   global   under   echo   content   error   版本管理   url   

1. 系统设置环境变量

? ~ which python3

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

? ~ echo $PATH

/Users/abc/.nvm/versions/node/v8.13.0/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Library/Apple/usr/bin

? ~ sudo vim /etc/bashrc

export PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:$PATH

2. pip加速

pip下载安装Python库很慢,可使用国内镜像网站加速,操作方法有二。

方法一:命令行使用参数项

pip install -i https://pypi.douban.com/simple/ sqlalchemy

方法二:命令行使用参数项

pip添加配置文件

  1. 查看用户主目录下有没有"~/.pip/pip.conf"文件,没有则创建。
 mkdir .pip
 touch .pip/pip.conf
  1. 打开文件,添加以下配置项
[global] 
index-url=http://pypi.douban.com/simple
[install] 
trusted-host=pypi.douban.com

3. 可以选择的镜像网址

  1. 豆瓣
[global] 
index-url=http://pypi.douban.com/simple 
[install] 
trusted-host=pypi.douban.com
  1. 阿里云
[global] 
index-url=http://mirrors.aliyun.com/pypi/simple 
[install] 
trusted-host=mirrors.aliyun.com
  1. 清华大学
[global] 
index-url=https://pypi.tuna.tsinghua.edu.cn/simple 
[install] 
trusted-host=pypi.tuna.tsinghua.edu.cn
  1. 中科大
[global] 
index-url=https://pypi.mirrors.ustc.edu.cn/simple/
[install] 
trusted-host=pypi.mirrors.ustc.edu.cn

4. 更换 pip 镜像源

  1. 临时使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
  1. 设为默认:
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

5. requirements.txt

  1. 在 A 环境生成 requirements.txt 文件:
$ env1/bin/pip freeze > requirements.txt
  1. 在 B 环境克隆 A 环境:
$ env2/bin/pip install -r requirements.txt

6. 创建虚拟环境,进行不同版本管理

  1. 进入tmp文件
?  / cd /tmp
  1. 创建虚拟环境venv1和venv2
?  /tmp python3 -m venv venv1
?  /tmp python3 -m venv venv2
  1. 进入虚拟环境venv1并安装requests
?  /tmp source venv1/bin/activate
(venv1) ?  /tmp pip3 install requests
  1. 在打开一个终端窗口,进入虚拟环境venv2
?  ~ cd /tmp
?  /tmp source venv2/bin/activate

引入requests

(venv2) ?  /tmp python3
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named ‘requests‘

python 设置环境变量和虚拟机环境以及pip 加速

标签:aliyun   默认   global   under   echo   content   error   版本管理   url   

原文地址:https://www.cnblogs.com/cloudwas/p/13207898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!