1. 程式人生 > >python pip及conda指南

python pip及conda指南

最近做實驗,換了新環境。OSX + Pycharm + Anaconda2
之前在Ubuntu上都是直接在終端中使用pip安裝相關的包。但是在OSX中卻會遇到一些坑。同時Anaconda2本身自帶了很多有用的包,加上conda的包管理,總體來說比pip優秀一些。但是conda安裝也會有些坑,這裡介紹一些今天安裝的經驗。

conda的安裝,這一塊就不介紹了,網上有非常多的教程。就是普通命令列下需要將conda新增到~/.bash_profile這個配置環境中才能使用。我是直接在pycharm中的終端使用conda安裝相關包的。

Conda配置

  1. conda配置國內源
# 使用清華的源是很好用的
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes

2.conda使用

# 檢視安裝的包
conda list
# 安裝conda的包
conda install 包名
# 更新conda的包
conda update 包名
# 刪除conda的包
conda remove 包名 # 建立並激活虛擬環境 conda create --name snowflakes biopython # 啟用新環境 # 這將建立一個名為/envs/snowflakes的環境,該環境包括程式Biopython source activate snowflakes # 建立並激活新環境 # 這次建立和命名一個新的環境,並安裝不同版本的Python和兩個包命名為Astroid和Babel conda create --name bunnies python=3 astroid babel # 驗證當前環境 conda info --envs # 刪除環境 conda remove --name flowers --all

Pip配置

  1. pip配置國內源(pip也要配置國內源,要不慢死)
在 ~/.pip/pip.conf (若沒有就建立它)新增以下內容
【豆瓣源】
[global]
timeout = 60
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
OR
[global]
timeout = 60
index-url = https://pypi.doubanio.com/simple
OR
【阿里源】
[global]
timeout = 60
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
  1. pip使用
# 如果pip沒新增到配置環境
python -m pip 相關操作
# 安裝包
pip install SomePackage # 最新版本
pip install SomePackage==1.0.4     # 指定版本
pip install 'SomePackage>=1.0.4'     # 最低的版本
# 安裝包(offline)
pip install SomePackage.whl
# 展示了已經安裝的包
pip show --files SomePackage
# 檢視已經過時的包
pip list --outdated
# 更新包
pip install --upgrade SomePackage
# 解除安裝包
pip uninstall SomePackage
# 安裝requirements中的包(具體requirements的格式接下來再說)
pip install -r requirements.txt
# 從指定本地目錄安裝requirements檔案
pip install wheels
pip wheel --wheel-dir=/local/wheels -r requirements.txt
# 從指定本地目錄安裝requirements檔案,不用pypi
pip install --no-index --find-links=/local/wheels -r requirements.txt
引數介紹
#
--user OR -U 針對特定(全部)使用者(如果加上這個引數,那麼pip安裝的包conda也能用!劃重點!!!)
--ignore-installed 強制安裝
PS:某些特殊情況下,需要使用強制安裝
  1. requirements的使用
# 格式
[[--option]...]
<requirement specifier> [; markers] [[--option]...]
<archive url/path>
[-e] <local project path>
[-e] <vcs project url>

# 例子

####### example-requirements.txt #######
#
###### Requirements without Version Specifiers ######
nose
nose-cov
beautifulsoup4
#
###### Requirements with Version Specifiers ######
#   See https://www.python.org/dev/peps/pep-0440/#version-specifiers
docopt == 0.6.1             # Version Matching. Must be version 0.6.1
keyring >= 4.1.1            # Minimum version 4.1.1
coverage != 3.5             # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1        # Compatible release. Same as >= 1.1, == 1.*
#
###### Refer to other requirements files ######
-r other-requirements.txt
#
#
###### A particular file ######
./downloads/numpy-1.9.2-cp34-none-win32.whl
http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
#
###### Additional Requirements without Version Specifiers ######
#   Same as 1st section, just here to show that you can put things in any order.
rejected
green
#

# 將相關包加入到requirements
pip SomePackage > requirements.txt