Build a python development environment under Ubuntu (Pycharm,postgresql,virtualenv, Django)

Source: Internet
Author: User
Tags postgresql psql install django postgres createdb postgres database virtual environment virtualenv


The system and software used

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

Before you start, be sure to make a backup of your system. Because if the operation is wrong, there will be unnecessary trouble. I just mistakenly installed PostgreSQL, and then there was a big problem, and finally had to put the system to reload.

The Ubuntu system comes with Python 2.7, so you don't have to use the special settings directly. Of course, because Django 1.4 is used, Python 3.0 or later is not available. So you don't have to reinstall Python.

The first step is to install the JDK.
Because Pycharm is written in Java, you must install the JDK to run it. If you have previously installed a JDK, you can skip this step. The following methods are provided to install and upgrade the JDK.

First, for insurance purposes, you need to remove the previously installed versions of the earlier version. The command line is as follows:
sudo apt-get purge openjdk*

If the JDK you previously installed is from another PPA, you need to do the following steps to install the new 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

Finally, start installing Oracle Java 7

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

After that, the installation is complete.

Step two, install Virtualenv and Virtualenvwrapper
The virtualenv was installed primarily to create a separate Python development environment. This allows the creation of multiple environments with different requirements on a single machine. You can create an environment with different versions of the program. For example, you can build a Django 1.4 environment, or you can build a Django 1.3 environment that doesn't affect the environment. And because you can not use system-level packages, the entire system is not cluttered because of small problems.

The installation of the virtualenv is simple

Pip Install Virtualenv

After the installation is complete, then install the Virtualenvwrapper
Enter command line
Pip Install Virtualenvwrapper

Step two, create a virtual environment

Virtualenv ENV
#ENV for the environment's name, can be arbitrarily set, in fact, is a folder, under the home under the User name folder can be found.

SOURCE Env/bin/activate
#This way into the virtual development environment of virtualenv.
After entering the virtual environment, the command line will start to show (ENV), the representative has entered the environment, then you can install Django


Step three, install Django

This is not the case, as long as you are still in the VIRTUALENV environment, the steps to install Django are exactly the same as the steps to actually install Django. You can refer to the steps of the official website. It's actually downloading, and then entering the command line.

Attached website address and method
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 does this, or you can use a gui-based tool such as 7-zip.
3. Change to 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 She ll prompt. If you ' re using Windows, start a command shell with administrator privileges and run the Commandpython setup.py install. This would install Django in your Python installation ' s site-packages directory.

After installing Django, use the Deactivate command to exit virtualenv.

Fourth, installing PostgreSQL

The system and software used

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

Before you start, be sure to make a backup of your system. Because if the operation is wrong, there will be unnecessary trouble. I just mistakenly installed PostgreSQL, and then there was a big problem, and finally had to put the system to reload.

The Ubuntu system comes with Python 2.7, so you don't have to use the special settings directly. Of course, because Django 1.4 is used, Python 3.0 or later is not available. So you don't have to reinstall Python.

The first step is to install the JDK.
Because Pycharm is written in Java, you must install the JDK to run it. If you have previously installed a JDK, you can skip this step. The following methods are provided to install and upgrade the JDK.

First, for insurance purposes, you need to remove the previously installed versions of the earlier version. The command line is as follows:
sudo apt-get purge openjdk*

If the JDK you previously installed is from another PPA, you need to do the following steps to install the new 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

Finally, start installing Oracle Java 7

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

After that, the installation is complete.

Step two, install Virtualenv and Virtualenvwrapper
The virtualenv was installed primarily to create a separate Python development environment. This allows the creation of multiple environments with different requirements on a single machine. You can create an environment with different versions of the program. For example, you can build a Django 1.4 environment, or you can build a Django 1.3 environment that doesn't affect the environment. And because you can not use system-level packages, the entire system is not cluttered because of small problems.

The installation of the virtualenv is simple

Pip Install Virtualenv

After the installation is complete, then install the Virtualenvwrapper
Enter command line
Pip Install Virtualenvwrapper

Step two, create a virtual environment

Virtualenv ENV
#ENV for the environment's name, can be arbitrarily set, in fact, is a folder, under the home under the User name folder can be found.

SOURCE Env/bin/activate
#这样进进入了virtualenv的虚拟开发环境.

After entering the virtual environment, the command line will start to show (ENV), the representative has entered the environment, then you can install Django


Step three, install Django

This is not the case, as long as you are still in the VIRTUALENV environment, the steps to install Django are exactly the same as the steps to actually install Django. You can refer to the steps of the official website. It's actually downloading, and then entering the command line.

Attached website address and method
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 does this, or you can use a gui-based tool such as 7-zip.
3. Change to 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 She ll prompt. If you ' re using Windows, start a command shell with administrator privileges and run the Commandpython setup.py install. This would install Django in your Python installation ' s site-packages directory.

After installing Django, use the Deactivate command to exit virtualenv.

Fourth, installing PostgreSQL


1. Installing PostgreSQL



Enter the following command



sudo apt-get install PostgreSQL






You will be prompted to install the required disk space, enter "Y" and the Setup program will complete automatically. After installation, the system creates a database Superuser "Postgres" with a blank password. This user is both a non-logged operating system user and a database user.





2. Modify the password for Linux user Postgres



Enter the following command



sudo passwd postgres



3. Modify the password of the database super user Postgres



1) switch to Linux under Postgres user



sudo su postgres



2) Log in to the Postgres database





Psql Postgres


This way you will see the Postgres prompt message as follows:



Psql (8.4.4)



Type ' help ' for help.



and the command line prompt symbol for Postgres appears:



postgres=#



3) Enter the following command



ALTER USER postgres with PASSWORD ' PASSWORD '



type "Exit" to return to the Linux command line.





4. Add your own defined users and databases



1) Add New user



CREATEUSER-DRSP Fedoraadmin



follow the prompts to enter the user's password.



2) Create a database that belongs to the custom user Fedoraadmin



Createdb-o Fedoraadmin MyDB



The Postgressql database can be connected in Java through the following configuration, as set up above



User:fedoraadmin



Password:your Password



Url:jdbc:postgresql://localhost:5432/mydb





5. Installing pgAdmin3



1) Type the following command to install PgAdmin3



sudo apt-get install pgadmin3



2) Type the following command to run PgAdmin3



Pgadmin3



you will see that the main interface of PgAdmin3 is as follows:





Add the appropriate parameters to create a connection to PostgreSQL:





6. Setting up access to postgres on other machines



Modify/etc/postgresql/8.4/main/pg_hba.conf:



Host all 0.0.0.0/0 MD5 #0.0.0.0 is the address segment, and 0 is the number of bits



For example: 192.168.0.0/16 represents 192.168.0.1-192.168.255.254





Modify/etc/postgresql/8.4/main/postgresql.conf



listen_address = ' * '





Restart database



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



Because Ubuntu 12.10 comes with Postgresql 9.1, you don't have to download it, you can install it directly in the terminal by typing the command line.
The command line is as follows:

sudo apt-get install postgresql-9.1

Then install the necessary packages, and attach the website's introduction and website. Some packages may have been installed before, but for the sake of insurance, install one side according to the official introduction.
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

Just replace the postgresql-9.1 in the command line with the name of the next bread. For example, if you need to install postgresql-client-9.1, enter

sudo apt-get install postgresql-client-9.1
The following are all the same.

After you install PostgreSQL, you need to make some settings for the database, such as adding role, creating a database, and so on. Here's how:

Set the user and password for PostgreSQL
Sudo-u Postgres CreateUser

Then follow the prompts to add the user
The first hint is to enter a user name and then ask if the user is a superuser, is not allowed to create a database, is not allowed to add new users. You can create a user by answering as needed.

Create a database
Sudo-u Postgres Createdb MyDB

#mydb is the name of the database and can be set as you wish

After creation, use the Psql command to set the password of the user you just created, and give this user permission to access the database
Sudo-u postgres psqlpostgres=# alter user Linuxpoison with encrypted password ' password ';
ALTER ROLE
postgres=# Grant all privileges on the database linuxdb to Linuxpoison;
GRANT

You can then use \l to see the database that has been successfully created and the user who has just been added and has access to the database.


Fifth Step, install PSYCOPG2

Need to re-enter the VIRTUALENV environment just now.

SOURCE Env/bin/activate

Then in the virtual environment, enter
Pip Install PSYCOPG2

The installation will be complete.
When you need to use the data, for example in Django settings.py, add the import psycopg2. Then add postgresql_psycopg2 at the end of the database engine.


Sixth Step, install Pycharm

Pycharm actually can be used as long as it is downloaded. But a bit different is that in the Ubuntu system, you need to run the pycharm.sh in the Bin folder to run Pycharm.

Without a special setting, Pycharm will use the system's Python environment by default instead of the virtualenv we just established as the development environment. Therefore, further setup is required to allow Pycharm to use the virtual environment. The method is as follows, because the official gives a very detailed method, so I directly put the URL and the content posted.

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 would be created.
* FROM Base interpreter Drop-down list, select one of the configured Python interpreters, which'll be used as the base F or the new virtual environment.
* If you want the site-packages of the base interpreter to is visible from the virtual environment, select the check box I Nherit Global site-packages. If You leave this check box cleared, the new virtual environment would be completely isolated.
* 2.6+ If want to assign the new virtual environment to being the project interpreter, make sure that the corresponding C Heck box is selected. Also, you can make this virtual environment available to all projects, same as if an existing virtual environment is add Ed.


At this point, pycharm on the Ubuntu development environment, even if the building is complete. You can develop a Python project in a virtual environment as long as you select the VIRTUALENV environment when you create a new project.
Because Ubuntu 12.10 comes with Postgresql 9.1, you don't have to download it, you can install it directly in the terminal by typing the command line.
The command line is as follows:

sudo apt-get install postgresql-9.1

Then install the necessary packages, and attach the website's introduction and website. Some packages may have been installed before, but for the sake of insurance, install one side according to the official introduction.
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

Just replace the postgresql-9.1 in the command line with the name of the next bread. For example, if you need to install postgresql-client-9.1, enter

sudo apt-get install postgresql-client-9.1
The following are all the same.

After you install PostgreSQL, you need to make some settings for the database, such as adding role, creating a database, and so on. Here's how:

Set the user and password for PostgreSQL
Sudo-u Postgres CreateUser

Then follow the prompts to add the user
The first hint is to enter a user name and then ask if the user is a superuser, is not allowed to create a database, is not allowed to add new users. You can create a user by answering as needed.

Create a database
Sudo-u Postgres Createdb MyDB

#mydb is the name of the database and can be set as you wish

After creation, use the Psql command to set the password of the user you just created, and give this user permission to access the database
Sudo-u postgres psqlpostgres=# alter user Linuxpoison with encrypted password ' password ';
ALTER ROLE
postgres=# Grant all privileges on the database linuxdb to Linuxpoison;
GRANT

You can then use \l to see the database that has been successfully created and the user who has just been added and has access to the database.


Fifth Step, install PSYCOPG2

Need to re-enter the VIRTUALENV environment just now.

SOURCE Env/bin/activate

Then in the virtual environment, enter
Pip Install PSYCOPG2

The installation will be complete.
When you need to use the data, for example in Django settings.py, add the import psycopg2. Then add postgresql_psycopg2 at the end of the database engine.


Sixth Step, install Pycharm

Pycharm actually can be used as long as it is downloaded. But a bit different is that in the Ubuntu system, you need to run the pycharm.sh in the Bin folder to run Pycharm.

Without a special setting, Pycharm will use the system's Python environment by default instead of the virtualenv we just established as the development environment. Therefore, further setup is required to allow Pycharm to use the virtual environment. The method is as follows, because the official gives a very detailed method, so I directly put the URL and the content posted.

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 would be created.
* FROM Base interpreter Drop-down list, select one of the configured Python interpreters, which'll be used as the base F or the new virtual environment.
* If you want the site-packages of the base interpreter to is visible from the virtual environment, select the check box I Nherit Global site-packages. If You leave this check box cleared, the new virtual environment would be completely isolated.
* 2.6+ If want to assign the new virtual environment to being the project interpreter, make sure that the corresponding C Heck box is selected. Also, you can make this virtual environment available to all projects, same as if an existing virtual environment is add Ed.


At this point, pycharm on the Ubuntu development environment, even if the building is complete. You can develop a Python project in a virtual environment as long as you select the VIRTUALENV environment when you create a new project.


Build a python development environment under Ubuntu (Pycharm,postgresql,virtualenv, Django)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.