zoukankan      html  css  js  c++  java
  • 在ubuntu下搭建python开发环境(pycharm,postgresql,virtualenv, Django)

    最近在用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项目。 

  • 相关阅读:
    对健康的一些思考
    对提问的一些思考
    UVA 10118 Free Candies(免费糖果)(dp记忆化搜索)
    UVA 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp记忆化搜索)
    UVA 12186 Another Crisis(工人的请愿书)(树形dp)
    UVA 10003 Cutting Sticks(切木棍)(dp)
    UVA 11584 Partitioning by Palindromes(划分成回文串)(dp)
    【洛谷P1144】最短路计数【最短路】
    【洛谷P1144】最短路计数【最短路】
    【洛谷P1144】最短路计数【最短路】
  • 原文地址:https://www.cnblogs.com/lddhbu/p/2881537.html
Copyright © 2011-2022 走看看