1. 程式人生 > >在ubuntu下搭建python開發環境

在ubuntu下搭建python開發環境


我使用的系統及軟體

Ubuntu 12.10
Python 2.7.3
Django 1.4.2
Pycharm 2.6.3
Postgresql 9.1
Virtualenv
Virtualenvwrapper
Openjdk

在開始之前,一定要給系統做一下備份。因為如果誤操作了會有不必要的麻煩。我就是誤安裝了Postgresql,然後出現了大問題,最後不得不把系統給重灌了。

Ubuntu的系統自帶python 2.7,所以不用特別的設定,直接用就行。當然因為使用了Django 1.4,也無法使用Python 3.0 以上版本。所以不用重新安裝Python。

第一步,安裝JDK。
因為pycharm是用Java編寫的,所以必須要安裝JDK才可以執行。如果以前已經安裝過JDK,可以跳過這一步。以下提供的方法是安裝以及升級JDK。

首先,為了保險,需要將之前已經安裝的低版本刪除。命令列如下:
sudo apt-get purge openjdk*

如果之前安裝的JDK是來自其他PPA,需要做以下步驟來安裝新的JDK

sudo rm /var/lib/dpkg/info/oracle-java7-installer*sudo apt-get purge oracle-java7-installer*
sudo rm /etc/apt/sources.list.d/*java*
sudo apt-get update

最後開始安裝 Oracle Java 7

sudo add-apt-repository ppa:webupd8team/javasudo apt-get update
sudo apt-get install oracle-java7-installer

之後就安裝完成了。

第二步,安裝Virtualenv和Virtualenvwrapper
安裝virtualenv主要是為了建立一個獨立的python開發環境。這樣可以在一臺機器上建立多個有不同需求的環境。可以建立有不同版本程式的環境。比方說可以搭建一個Django 1.4的環境,也可以搭建Django 1.3的環境,兩個環境之間互不影響。而且因為可以不使用系統級別的包,不會因為小問題導致整個系統混亂。

virtualenv的安裝很簡單

pip install virtualenv

安裝完以後,建立一個虛擬環境,然後在安裝virtualenvwrapper

virtualenv ENV
#ENV 為環境的名字,可以任意設定,其實就是一個資料夾,在home下的使用者名稱資料夾下可以找到。

source ENV/bin/activate
#這樣進進入了virtualenv的虛擬開發環境。

進入虛擬環境以後命令列最開始的地方會顯示(ENV),代表已經進入了這個環境,然後就可以安裝virtualenvwrapper和Django了

輸入命令列
pip install virtualenvwrapper

這裡可以不用sudo,因為在virtualenv裡,不用管理許可權也算是很方便的設計之一。

第三步,安裝Django

這裡就不贅述了,只要還在virtualenv環境裡,安裝Django的步驟跟實際安裝Django的步驟完全一樣。可以參考官網的步驟。其實就是下載,然後輸入命令列的事。

附上官網地址和方法

https://docs.djangoproject.com/en/1.4/topics/install/#installing-a-distribution-specific-package


1. Download the latest release from our download page.
2. Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, where X.Y is the version number of the latest release). If you're using Windows, you can download the command-line tool bsdtar to do this, or you can use a GUI-based tool such as 7-zip.
3. Change into the directory created in step 2 (e.g. cd Django-X.Y).
4. If you're using Linux, Mac OS X or some other flavor of Unix, enter the command sudo python setup.py install at the shell prompt. If you're using Windows, start a command shell with administrator privileges and run the commandpython setup.py install. This will install Django in your Python installation's site-packages directory.

安裝完Django 以後,用deactivate命令,退出virtualenv。

第四部,安裝Postgresql
因為Ubuntu 12.10自帶 Postgresql 9.1, 就不用下載了,直接在terminal 裡輸入命令列就可以安裝。
命令列如下:

sudo apt-get install postgresql-9.1

然後安裝必要的包,附上官網的介紹及網址。有些包在之前可能已經被安裝過了,但是保險起見,還是按照官方的介紹安裝一邊。
http://www.postgresql.org/download/linux/ubuntu/



* postgresql-client-9.1 - client libraries and client binaries
* postgresql-9.1 - core database server
* postgresql-contrib-9.1 - additional supplied modules
* libpq-dev - libraries and headers for C language frontend development
* postgresql-server-dev-9.1 - libraries and headers for C language backend development
* pgadmin3 - pgAdmin III graphical administration utility

只要把命令列裡的postgresql-9.1 替換為下面包的名字即可。比方說,需要安裝postgresql-client-9.1,就輸入

sudo apt-get install postgresql-client-9.1
下面的都一樣。

安裝完postgresql以後,需要對資料庫進行一些設定,比方說新增role,以及建立資料庫等。具體方法如下:

設定postgresql 的使用者以及密碼
sudo -u postgres createuser

然後按照提示新增使用者
第一個提示是輸入使用者名稱,然後問這個使用者是不是超級使用者,是不是允許建立資料庫,是不是允許新增新的使用者。按照需要回答,就可以建立一個使用者。

建立一個數據庫
sudo -u postgres createdb mydb

#mydb 是資料庫的名字,可以按自己意願設定

建立完以後用psql命令設定剛剛建立的使用者的密碼,以及賦予這個使用者許可權訪問資料庫
sudo -u postgres psqlpostgres=# alter user linuxpoison with encrypted password 'password';
ALTER ROLE
postgres=# grant all privileges on database linuxdb to linuxpoison;
GRANT

之後可以使用\l看到已經成功建立的資料庫以及這個剛剛被新增的使用者以及有了許可權訪問這個資料庫。


第五步,安裝psycopg2

需要重新進入剛才的virtualenv的環境。

source ENV/bin/activate

然後在虛擬環境下,輸入
pip install psycopg2

就可以安裝完成了。
在需要使用到資料的時候,比方說在Django的settings.py裡,加上import psycopg2即可。然後在DATABASE的ENGINE裡的末尾加上postgresql_psycopg2即可。


第六步,安裝pycharm

pycharm其實只要下載下來既可以使用。但是有點不同的是,在Ubuntu系統裡,需要執行bin資料夾裡的pycharm.sh來執行Pycharm。

如果沒有特別的設定,pycharm會預設使用系統的Python環境,而不是用我們剛剛建立的virtualenv作為開發環境。所以需要進一步設定,來讓Pycharm使用虛擬環境。具體方法如下,因為官方給出了非常詳細的方法,所以我就直接把網址和內容貼過來了。

http://www.jetbrains.com/pycharm/webhelp/creating-virtual-environment.html



1. Open the project settings, and click Python Interpreters page.
2. Click in the toolbar.
Create New Virtual Environment dialog box opens.

3. In the Create New Virtual Environment dialog box:
* In the Name field, type the name of the new virtual environment, or accept the suggested default name.
* In the Location field, specify the target directory, where the new virtual environment will be created.
* From Base interpreter drop-down list, select one of the configured Python interpreters, which will be used as the base for the new virtual environment.
* If you want the site-packages of the base interpreter to be visible from the virtual environment, select the check box Inherit global site-packages. If you leave this check box cleared, the new virtual environment will be completely isolated.
* 2.6+ If you want to assign the new virtual environment to be the project interpreter, make sure that the corresponding check box is selected.Also, you can make this virtual environment available to all projects, same as when an existing virtual environment is added.


至此,pycharm在ubuntu的上的開發環境就算搭建完成了。只要在建立新的專案的時候選擇virtualenv環境,即可在虛擬環境下開發python專案。