The way of Python 1-Environment building and pycharm configuration django installation and MySQL database configuration

Original link: https://www.cnblogs.com/hwtmhj/p/6746151.html

I have been developing that python recently. Today, I will briefly write about the installation and configuration of the development route.

Development route Python3.6.1+Pycharm5.0.6+Django1.11+MySQL5.7.18

1- Install Python3.6.1

  The syntax of Python2.x and 3.x is slightly different, the difference lies in the difference of the output statement, which can be seen in the relevant documents.

  Python3.6.1, which can be downloaded from Python's official website: https://www.python.org/downloads/,

Successful installation:

  After downloading, install it directly. When you are finished, test to see if Python is configured in the environment variable. Computer-->Properties-->Advanced System Settings-->Environment Variables-->

System environment variable --> path path, find the path of the Python.exe file under the installation directory, add it under the path path, and separate it with a semicolon. For example, I have to configure as shown in Figure 1:

 

               Figure 1 Add python to the environment variable              

 

               Figure 2 Test whether Python is installed successfully

After that, test whether the installation is under the dos window, and use the shortcut key win+R to open the dos window: enter Python and it will appear as shown in Figure 2, and the installation is successful.

2- Install pycharm5.0.6

  PyCharm is a Python IDE with a complete set of tools that can help users improve their efficiency when developing in the Python language, such as debugging, syntax highlighting,

Project management, code jumping, smart prompts, auto-completion, unit testing, version control. Additionally, the IDE provides some advanced features for supporting Django boxes

Professional web development off the shelf. Its interface design is friendly, and various class libraries can be installed in pycharm, which is fast and convenient, and it is very comfortable to use. pycharm download address can be

Download it by yourself on the official website, download address: https://www.jetbrains.com/pycharm/download/#section=windows, select the corresponding version for

download.

  After pycharm is downloaded, you can install it with one click, or you can adjust the installation path yourself, then open pycharm and introduce the newly installed interpreter:

file--setting--project--project Interpreter is shown in Figure 3: 

                                                                 Figure 3 Introducing the interpreter path into pycharm

  This when you install Python, pycharm will automatically detect the interpreter, and it will be automatically added to Road King, and then open the terminal under pycharm,

Enter Python to check whether the installation is successful. As shown in Figure 4, the installation is successful:  

                                               Figure 4 Whether the test under the pycharm terminal is successful

  Now write a very simple test example: create a new python file file--new--Python package, name the new python file test.py, open the newly created file,

Write the following code as shown in Figure 5,

 

                                        Figure 5 Test code

Click Run, and the operation result shown in Figure 6 appears, which means that everything is ready to install, just wait and roll up your sleeves and code.

                                       Figure 6 Running results

3 Install Django1.11

  Django is an open source web application framework written in Python. The software design pattern of MVC is adopted, namely model M, view V and controller C. it was originally

CMS (Content Management System) software was developed to manage some of the news content-focused websites under the Lawrence Publishing Group. and licensed on BSD in July 2005

证下发布。这套框架是以比利时的吉普赛爵士吉他手Django Reinhardt来命名的。Django1.11下载https://www.djangoproject.com/download/

选择对应的版本进行下载。因为Django本身是由Python编写,所以先要安装Python,参考上面的步骤。这里介绍如何在windows下安装Django。

  方法一:在dos窗口下采用压缩包的的方式安装

  Django下载后为压缩包,解压缩跟Python放在同一个根目录,在dos下进入到Django-1.11目录,执行python setup.py install,

然后开始安装,Django将要被安装到Python的Lib下site-packages。安装好之后如下如所示:

然后是配置环境变量,将这几个目录添加到系统环境变量中:

D:\Program Files\python3.6.1\Lib\site-packages\django; 

D:\Program Files\python3.6.1\Scripts;

检查是否安装成功,可以在dos下进入Django目录查看Django版本。

1.输入python ,

2.输入import django,

3.输入django.get_version(),

出现如上图所示的则表明Django安装成功。

  方法二:在pycharm中利用其强大的类库进行安装。

  首先打开pycharm,点击左上角File-->Setting-->Project-->Project Interface,然后点击右上角+(install)按钮,

在搜索框中输入Django,最后点击最下边的install package,进行安装。

 

 安装成功后如下图所示:

  在pycharm终端terminal下面测试看是否Django是否安装成功,如下图:

出现如图所示,则表示Django安装成功。

  方法三:采用Python自带的pip命令进行安装:

  之前已经将D:\Program Files\python3.6.1\Scripts;添加到环境变量中,打开dos窗口 输入pip命令,如下显示:

在pip命令下输入 pip install django:

我的电脑早已安装Django成功,会出现如图所示。

  在pycharm中新建Django工程,file-->new project-->Django,填写相应的工程名字如下所示:

创建好的工程文件目录如图所示:

直接在终端下输入 Python manage.py runserver,如图所示则表示服务器启动成功:

然后再浏览器中输入本地地址:http://127.0.0.1:8000/ 出现如下图所示,则表示Django安装成功:

4-安装数据库MySQL5.7.18

  第一步:数据库MySQL5.7.18可以在官网上下载对应的版本,下载地址:http://www.filehorse.com/download-mysql-64/,

  第二步:将下载好的安装包进行解压到一个盘下面,

  第三步:配置环境变量,新建环境变量MySQL_HOME,输入MySQL的安装目录,

然后再把;%MYSQL_HOME%\bin插入到Path的最后面;

  第四步:以管理员的身份运行命令行,按win+R 输入cmd,点鼠标右键以管理员身份运行。

  第五步:切换到MySQL的安装目录下面,利用mysqld 进行安装:

  初始化数据: mysqld --initialize-insecure --user=mysql,

  启动服务: mysqld --user=mysql,

  初始化mysql root密码 mysqladmin -u root password ‘new-password',

  详情见https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html 。 

  第六步:注册服务mysqld --install MySQL

  用net start mysql 命令开启服务。

   输入mysql -u root -p命令: 然后再填写第五步初始化数据库时输入的密码,如下图所示: 

在mysql命令下输入 show databases; ,查看数据库中的表,如下图所示:

这说明数据库已经安装成功。

5-MySQL和Python的连接,也是最重要的一个环节。

  Python2.x系列连接MySQL数据库需要MySQLdb类库的支持,Python3.x系列连接MySQL需要pymysql类库的支持,

  python3.6安装模块pymysql:

  1.命令行安装  pip install pymysql,

  2.使用pycharm支持的类库安装,安装过程和Django安装过程的方法二一样,在此不再重复说明,如有问题可参照Django安装的方法二。

所有需要的安装包安装完之后,可以在pycharm里面查看,如下图所示:

  接下来用代码实现MySQL和Python的连接:

  在setting文件中找到数据库的配置代码,Django工程中。数据库一般默认的SQLite数据库,如下所示:

使用MySQL数据亏更改代码如下所示:

  NAME: 指定的数据库名,如果是sqlite的话,就需要填数据库文件的绝对位置
  USER: 数据库登录的用户名,mysql一般都是root
  PASSWORD:登录数据库的密码,必须是USER用户所对应的密码
  HOST: 由于一般的数据库都是C/S结构的,所以得指定数据库服务器的位置,我们一般数据库服务器和客户端都是在一台主机 上面,所以一般默认都填127.0.0.1
  PORT:数据库服务器端口,mysql默认为3306
  HOST和PORT都可以不填,使用默认的配置,但是如果你有更改默认配置的话,就需要填入更改后的。
现在需要在配置文件目录下面的__init__.py 文件中写入连接的代码即可,如下图所示:

完成之后运行整个工程文件,在终端(terminal)下面输入命令:Python manage.py runserver 如下所示:

或者也可以直接点击Python console,显示如下的输出:

则表明数据库已经连接成功,整个开发环境也成功的搭建好了。

请注意连接数据库时要保证数据库已经开启,否则连接失败。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325582161&siteId=291194637
Recommended