在ubuntu下搭建python开发环境(pycharm,postgresql,virtualenv, Django)

使用的系统及软件

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
输入命令行
pip install virtualenvwrapper

第二步, 建立一个虚拟环境

virtualenv ENV
#ENV 为环境的名字,能够任意设置,其实就是一个文件夹,在home下的用户名文件夹下能够找到。

source ENV/bin/activate
#这样进进入了virtualenv的虚拟开发环境。

进入虚拟环境之后命令行最开始的地方会显示(ENV),表明已经进入了这个环境,而后就能够安装Django了


第三步,安装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 html

使用的系统及软件

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
输入命令行
pip install virtualenvwrapper

第二步, 建立一个虚拟环境

virtualenv ENV
#ENV 为环境的名字,能够任意设置,其实就是一个文件夹,在home下的用户名文件夹下能够找到。

source ENV/bin/activate
#这样进进入了virtualenv的虚拟开发环境。

进入虚拟环境之后命令行最开始的地方会显示(ENV),表明已经进入了这个环境,而后就能够安装Django了


第三步,安装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 java

1. 安装PostgreSQLpython

  输入以下命令linux

 sudo apt-get install postgresqlweb

 

系统会提示安装所需磁盘空间,输入"y",安装程序会自动完成。 安装完毕后,系统会建立一个数据库超级用户“postgres”, 密码为空。这个用户既是不可登陆的操做系统用户,也是数据库用户。sql

 

2. 修改Linux用户postgres的密码shell

  输入以下命令数据库

 sudo passwd postgresdjango

3. 修改数据库超级用户postgres的密码ubuntu

1) 切换到Linux下postgres用户

 sudo su postgres

2) 登陆postgres数据库

 

psql postgres

这样你会看到postgres提示信息以下:

psql (8.4.4)

Type "help" for help.

并出现postgres的命令行提示符号:

postgres=#

3) 输入以下命令

 ALTER USER postgres with PASSWORD 'password'

 键入“exit”返回到Linux命令行。

 

4. 添加本身定义的用户和数据库

1) 添加新用户

 createuser -drSP fedoraAdmin

   按照提示输入该用户的密码。

2) 建立一个属于自定义用户fedoraAdmin的数据库

 createdb -O fedoraAdmin mydb

经过如上设置,能够在Java中经过如下配置来链接PostgresSQL数据库

user:fedoraAdmin

password: your password

url: jdbc:postgresql://localhost:5432/mydb

 

5. 安装pgAdmin3

1) 键入以下命令安装pgAdmin3

 sudo apt-get install pgadmin3

2) 键入以下命令运行pgAdmin3 

 pgadmin3

 你就会看到pgAdmin3的主界面以下所示:

添加相应的参数以建立一个到PostgreSql的链接:

6. 设置其它机器上对postgres的访问

修改/etc/postgresql/8.4/main/pg_hba.conf:

host all all 0.0.0.0/0 md5  #0.0.0.0为地址段,0为多少二进制位

例如:192.168.0.0/16表明192.168.0.1-192.168.255.254

 

修改/etc/postgresql/8.4/main/postgresql.conf

listen_address = '*'

 

重启数据库

sudo /etc/init.d/postgresql-8.4 restart


由于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项目。
由于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项目。

相关文章
相关标签/搜索