1. 程式人生 > >Python Environment Management with Conda (Python 2 + 3, Using Multiple Versions of Python)

Python Environment Management with Conda (Python 2 + 3, Using Multiple Versions of Python)

Python Environment Management with Conda (Python 2 + 3, Using Multiple Versions of Python)

Environment Management with Conda (Python + Configuring Ipython/Jupyter)

Coming across an ImportError similar to the one in the image below can be annoying.

Luckily, Anaconda makes it easy to install packages with the package manager functionality of conda. In case you need a refresher, a package manager is a tool which automates the process of installing, updating, and removing packages. While Conda is a package and environment manager, let’s first review the package manager functionality of conda and then focus on the environment manager functionality.

Install Packages

Open a command prompt/anaconda prompt (windows) or a terminal (mac/linux) and run the command below. You can be substitute numpy for whatever package you want to install.

conda install numpy

Uninstall Packages

Run the command below to uninstall a package. You can be substitute numpy for whatever package you want to uninstall.

conda uninstall numpy

Update Packages

Run the command below to update a package. You can be substitute scikit-learn for whatever package you want to update.

conda update scikit-learn

Conda vs Pip

Pip is a Python package manager which stands for “Pip Installs Packages” that is usually coupled with virtualenv

(a tool for creating isolated environments).

Most of the time (with some exceptions) there isn’t much of a difference between installing packages through conda or through pip. This is because pip packages are also installable into Conda environments.

conda install numpy
pip install numpy

This isn’t a discussion on conda vs pip as Jake VanderPlas covered it pretty extensively, but why you can mostly install packages through either pip or conda.

Why you Need Multiple Conda/Python Environments.

Say you have multiple projects and they all rely on a library (Pandas, Numpy etc). If you upgrade one package it could break your other projects relying on old versions of the package as the old project’s syntax could become deprecated or obsolete. What should you do? Set up a virtual environment. It allows you to separate out packages, dependencies and versions you are going to use from project to project.

A common use of virtual environments for python users is having separate Python 2 and 3 environments. For example, a couple of my classmates at UC San Diego recently started a machine learning class where one professor sets assignments in Python 3. However, another class has a professor who sets assignments in Python 2. Consequently, they have to frequently switch between python 2 and 3 in their different class projects. With that, let’s get into conda environment commands. I recommend you open the video in a separate tab to watch the commands in action.

Create a New Environment

The command below creates a conda environment named subscribe in python version 3.6. You can be substitute subscribe for whatever you want to name your environment.

conda create --name subscribe python=3.6

Keep in mind that you will need to install additional packages inside that environment once you activate (enter) that environment. The packages you have in your root environment are not necessarily the ones you will have in your new environment unless you install them.

You can also create an environment with multiple packages in it. This also gives you the option to install additional packages later if you need them.

conda create --name chooseAnotherName python=3.6 numpy pandas scipy

Enter an Environment

If the name of your environment is not subscribe, you will need to substitute subscribe for the name of your environment.

Windows:

activate subscribe

Mac:

source activate subscribe

Leave an Environment

If the name of your environment is not subscribe, you will need to substitute subscribe for the name of your environment.

Windows:

deactivate subscribe

Mac:

source deactivate subscribe

List your Environments

This command shows you all the This is a really helpful command to see what environments you have, but also to see what conda environment you are in.

conda env list

Remove an Environment

If the name of your environment you want to remove is not subscribe, you will need to substitute subscribe for the name of your environment you want to remove.

conda env remove --name subscribe

If you have questions on this part, please refer to the documentation, leave a comment or refer to the video above.

Using Both Python 2.x and Python 3.x Environments in IPython Notebook

While this section of the post was largely taken and improved from stackoverflow, I feel like it is important to go over how and go over some technical issues people run into. The main idea is to have multiple ipython kernels. The package nb_conda_kernels will automatically detect different conda environments with notebook kernels and automatically register them.

  1. Make sure you have anaconda 4.1.0 or higher. Open a new terminal and check your conda version by typing
checking conda version

if you are below anaconda 4.1.0, type conda update conda

2. Next we check to see if we have the library nb_conda_kernels by typing

Checking if we have nb_conda_kernels

3. If you don’t see nb_conda_kernels type

Installing nb_conda_kernels

4. If you are using Python 2 and want a separate Python 3 environment please type the following

py36 is the name of the environment. You could literally name it anything you want.

If you are using Python 3 and want a separate Python 2 environment, you could type the following.

py27 is the name of the environment. It uses python 2.7.

5. Close your terminal and open up a new terminal. type jupyter notebook

6. Click new and you will see your virtual environment listed.

Please let me know if you have any questions either here or in the youtube video comments!

相關推薦

Python Environment Management with Conda (Python 2 + 3, Using Multiple Versions of Python)

Python Environment Management with Conda (Python 2 + 3, Using Multiple Versions of Python)Environment Management with Conda (Python + Configuring Ipython/J

Python 列表元素分組,比如 [1,2,3,...100]變成 [[1,2,3],[4,5,6]....](列表生成式解決)

range 列表生成式 元素 python 列表 for .... ... 分組 [88 In [29]: a=[x for x in range(1,101)] In [30]: b=[a[x:x+3] for x in range(0,100,3)] In [31]:

windows OpenCV 2.3.1/Opencv2.4.6 + Python 2.7配置

1 .下載 OpenCV 2.3.1 。文中下載了OpenCV-2.3.1-win-superpack (大概124MB,解壓後1G多)。他不需編譯,使用方便 下載地址  2. OpenCV-2.3.1-win-superpack.exe是自解壓檔案,直接執行。即可解壓。預設解壓到opencv資料夾裡。

[python高效能程式設計-學習筆記]章節2.3計算完整的Julia集合

Python高效能程式設計2014版中2.3採用了Julia集合作為範例來作為效能分析的範例,下面是作為一個python初階使用者在學習示例中遇到的一些問題,我在這裡把他們記錄下來,作為備忘。同時希望能夠幫助到後續學習該書的同學解決一些疑點。 首先是對Julia集合的介紹。

Python 練習一(計算1-2+3-4...+99)

想要 增加 偶數 減法 pre 記錄 相反數 span 保持 # 求1-99的所有數的和 count = 1 s = 0 while count < 100: s += count count += 1 print(s) 當都為正

Python實現groupBy函數。grpby = groupBy(lambda x: x%2 is 1),grpby([1, 2, 3])的結果為{True: [1, 3], False: [2]}

結果 false n) pen als 不存在 def lam nbsp def groupBy(fn): def go(lst): m = {} for v in lst: m[fn(v)].append(v) if m.get(fn(v

python入門:求1-2+3-4+5...99的所有數的和(自寫)

== 余數 奇數 nbsp int 當前 pre span bre 1 #!/usr/bin/env pyhton 2 # -*- coding:utf-8 -*- 3 #求1-2+3-4+5...99的所有數的和(自寫) 4 """ 5 給x賦值為0,給y賦值

python 1-2+3-4....99=? 這裏的題,我看到別人的寫法,五花八門的,自己也寫一個

blog sta 裏的 and str 想象力 pos 問題 就是 f = ""s = ""i = 1sum = 0while i < 100:   if i %2 == 1 and i < 99:     f = "-"

win64 Python下安裝PIL出錯解決2.7版本 (3.6版本可以使用)

tis code win 選擇 pill could not 出錯 win64 3.6 轉自:http://blog.csdn.net/lhh31/article/details/51979293 1、軟件版本 首先我先安裝了 python 2.7 pip是 8.1.2

python--練習--2-3+4-5+6...+100=?

python--練習#!/usr/bin/python3 n=3sum=2while n<=100 : if n%2 !=0 : sum=sum-n n=n+1 else : sum=sum+n n=n+1 print("2-3+4-5+6...+100=&qu

python入門:求1-2+3-4+5...99的所有數的和

== python入門 ... sta rt+ color python while style 1 start =1 2 sum =0 3 while start <100: 4 if start % 2 ==0: 5 sum =sum -

python 求階乘之和。求1+2!+3!+...+20!的和

blank HR IV sharp ML 術語 lis get 功能 階乘:也是數學裏的一種術語;階乘指從1乘以2乘以3乘以4一直乘到所要求的數;在表達階乘時,就使用“!”來表示。如h階乘,就表示為h!;階乘一般很難計算,因為積都很大。 分析:1、階乘的計算就是比較麻煩的一

2.3.10 Python 函數進階

臨時 名稱空間 函數定義 綁定 說明 查看 模塊 () globals 名稱空間 又名 name space。 說明:若變量 x=1, 1存放於內存中,那名字x存放在哪裏? 名稱空間正是存放名字x與1綁定關系的地方 名稱空間共三種: locals

python練習題,寫一個方法 傳進去列表和預期的value 求出所有變量得取值可能性(例如list為[1,2,3,4,5,6,12,19],value為20,結果是19+1==20只有一種可能性),要求時間復雜度為O(n)

num bubuko com pri def 代碼 data- 取值 .com 題目:(來自光榮之路老師)a+b==valuea+b+c=valuea+b+c+d==valuea+b+c+d+...=valuea和b....取值範圍都在0-value寫一個方法 傳進去列

Python開發【2.3 模塊】

重新 pan python開發 print code port highlight pre UNC 1、模塊導入 import 模塊名 from 模塊名 import 函數/類/變量 2.模塊路徑 import sys sys.path 3.模塊重新導入 fr

第4章:介紹python物件型別/4.1 python的核心資料型別/4.2 字串/4.2.3 字串查詢、替換、分解、轉大小寫

字串查詢/替換/分解/轉大小寫 字串查詢 >>> S 'abcd' >>> S.find("bc") 1 替換 >>> S.replace("bc","xyz") 'axyzd' 分解 >>>

python爬蟲系列(2.3-requests庫模擬使用者登入)

一、模擬登入拉鉤網 import re import requests class LoginLaGou(object):     """     模擬登入拉鉤網     """    

21天學python——第二章2.3

2.3.1算式與代數式運算 和C語言計算方法一樣,加減乘除括號完全一樣,這裡多了一個**乘方 就是2**3 == 2^3    而且python有一個math庫函式,具體方法如下,有些寫返回的就是一看看下去的作用,比如第一個就是返回歐幾里得距離的平方

2.3 Python語言基礎

opera 實現 -o recent ast 如果 traceback 功能 tin 2.3 Python語言基礎 1 語言語義(Language Semantics) 縮進,而不是括號 Python使用空格(tabs or spaces)來組織代碼結構,而不是像R,C++

一行python程式碼寫九九乘法表,1,2,3…到100的和,交換兩個變數的值

用一行程式碼輸出九九乘法表: 第一種方法: print('\n'.join([' '.join(["%d*%d=%d"%(i,j,i*j) for i in range(1,j+1)])for j in range(1,10)])) 第二種方法: print('\n'.join