在ubuntu下搭建Django开发环境

ubuntu搭建一个python的开发环境。因为遇到了不少问题,所以记下来已被以后查看。另外也希望遇到同样问题的开发者参考以解决问题。 

我使用的系统及软件 

Ubuntu 13.10 
Python 2.7.3 
Django 1.4.2 
Pycharm 2.6.3 
Mysql
Virtualenv 
Virtualenvwrapper 
Openjdk



在开始之前,一定要给系统做一下备份。因为如果误操作了会有不必要的麻烦。我就是误安装了Postgresql,然后出现了大问题,最后不得不把系统给重装了。 

Ubuntu的系统自带python 2.7,所以不用特别的设置,直接用就行。

第一步,安装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。 

第四部,安装Mysql
因为Ubuntu 12.10自带 MySql, 就不用下载了,直接在terminal 里输入命令行就可以安装。 
命令行如下: 

sudo apt-get install mysql-server
sudo apt-get install 
 
my-client




第五步 安装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项目。

转载于:https://my.oschina.net/zhangzhe/blog/414508

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值