Flask Framework Learning Notes (i) Installation article (Windows installation and CentOS installation) _python

Source: Internet
Author: User
Tags centos virtual environment virtualenv

Flask relies on two external libraries: Werkzeug and JINJA2. Werkzeug is a toolset for WSGI (standard Python interfaces developed and deployed between Web applications and multiple servers), and JINJA2 is responsible for rendering templates.

First, installation

Prerequisites for flask Installation

1. python2.x version has been installed

2. Installed Easy_install

Before installing flask, you have to install Python and Easy_install,easy_install only support the pyhon2.x version does not support the python3.x version, so you'd better choose python2.x when installing Python. This is 2.7.

python2.7 installation is very simple, this site has a lot of articles introduced, here without description, the path of arbitrary, installation Complete add environment variables can be.

Win7:

Configure Environment variables

Method One: (Computer-"attribute-" Advanced system Settings-"Environment variable-" Add Python installation path)

Method Two: Under the cmd

Set: Set pythonpath=%pythonpath%; C:\My_python_lib

View: Echo%PATH%

Easy_install Installation:

Download Address: Http://pypi.python.org/pypi/setuptools

Windows version will only have a ez_setup.py script, downloaded down to the D:\Python\python2.7 directory execution, will automatically install Easy_install, directory: D:\Python\python2.7\Scripts

Easy_install Add environment variable: path is D:\Python\python2.7\Scripts

Install the two, then start the installation of flask

Install Virtualenv, which is primarily used to isolate the interpreter environment, avoiding multiple python or multiple Python libraries on the same machine

Then configure the virtual environment

Then the CD to the scripts under the Myvir directory

Enter the Activate.bat, enter the virtual environment, and then type Easy_install flask installation

Test results, whether the installation was successful:

In the Pycharm software, you create the Flask project and then select the Python.exe run script in Myvir.

Create a simple Hello World script with file name test1.py:

From flask import Flask
app=flask (__name__)
@app. Route ('/')
def hello_world (): Return
"Hello world~~ ~ "
if __name__ = = ' __main__ ':
app.run ()

Then click Run and it will show

can be accessed by a given URL. Note: It is always running at this time, when finished to close the port then click on the Run->stop in Pycharm.
The default is only local access and the port is 5000.

The last line is modified to App.run (' 0.0.0.0 ', 12345) so that other people can access the

Analytical:

From flask import Flask

Import Flask Class

App = Flask (__name__)

Instantiate the object app, the parameter is the name of the application module or package, where __name__ refers to the __main__ main program. This parameter is required so that flask can know where to find

To templates and static files and things like that.

@app. Route ("/")

Use the route () adorner to tell flask the URL of the triggering function. Can be customized, such as @app. Route ("/test1.py"), and then the file name after access

def hello (): return

    "Hello world!"

A function that is defined to generate the associated URL and return information that needs to be displayed in the user's browser.

App.run ()

Run server application, after running the default only local access, if you need to allow other connections, you can specify host, such as: App.run (host= ' 0.0.0.0 ')

The default port used is: 5000, you can use a custom host and port: App.run (host= "0.0.0.0", port=8000)

Servers that can be accessed externally

If you run the server, you will notice that it can only be accessed from your own computer, and that no other place on the network can access it. This is the default, because in debug mode, the user can execute arbitrary Python code on your computer.

If you disable debug or trust a user on your network, you can simply modify the call to run () to make your server publicly available, as follows:

App.run (host= ' 0.0.0.0 ')

This will allow the operating system to listen for all exposed IP.

Second, install the 3.3 version under Windows:

Note: If 2.7 is installed by default and you want to install 3.3, you must go to the 3.3 installation directory and run the python ez_setup.py (ez_setup.py download Address: https://pypi.python.org/pypi/setuptools).

Then go to the newly generated scripts directory and perform the following command to install VIRTUALENV.

Similarly, the creation of the virtual environment also need to use 3.3 of the virtualenv, or error.

Then the CD to the scripts under the Myvir directory

Enter the Activate.bat, enter the virtual environment, and then type Easy_install flask installation

Iii. the Flask framework for centos6.4 installation python2.6::

To install the Execute command:

Yum install openssh-server
python--version (see if the version is compliant)
yum install python-setuptools
easy_install Virtualenv (System defaults to install easy_install2.6)

Once installed, you can immediately open the shell and create your own environment.

1. Global (not recommended):

Easy_install Flask Global installation, there are local installation methods below.

Test code:

From flask import flask 
app = Flask (__name__) 
@app. Route ('/') 
def hello_world (): Return 
"Hello world!" 
if __name__ = = ' __main__ ': 

The location of the file is not required and can be performed anywhere. (Global installation)
accessing from another server: the way of http://IP:5000
Then others, such as template rendering, also only need to create Templates folder in your project to achieve the way.

2. Local Environment:

I usually create a project folder and create a venv folder under it

[Root@localhost opt]# mkdir myweb

[root@localhost opt] #cd myweb

[root@localhost, myweb]# virtualenv Venv (Note: Here the Venv directory name is set by itself)
New python executable in Venv/bin/python
installing Setuptools, Pip...done.

Now, whenever you want to work on a project, you just need to activate the appropriate environment.

Benefits:

Everything has been installed in this virtual environment, so your own main Python installation environment will not be affected (you can support several environments at the same time). An added benefit is that you do not need root administrator permissions to install this way.
After migration, the execution of the file will not work because of changing the path, or you need to rebuild the environment.

On OS X and Linux, do the following:

[Root@localhost venv]#. Bin/activate   #激活 (each run has an active state)
(venv) [Root@localhost venv]# 

The following actions apply to Windows:

$ venv\scripts\activate

Either way, you should now have activated the VIRTUALENV (note that your shell prompt shows an active environment).

Now you only need to type the following command to activate the flask in virtualenv:

(venv) [root@localhost venv]# pip install flask             #只需开始执行一次 ...
Successfully installed Flask Werkzeug JINJA2 itsdangerous markupsafe up
...

After a few seconds, everything was done.
There may be an error:

Sslerror:the read operation timed out

Storing debug log for failure In/root/.pip/pip.log

This is the wrong thing to do if you just rerun the command.

Perform. Activate.csh can quit virtualenv (don't know if it's the right way, but you can actually quit)

Iv. the flask framework for installing python3.3 under CentOS:

If 3.3 of the Easy_install:

[Root@localhost python3.3.3]# wget https://bootstrap.pypa.io/ez_setup.py
[root@localhost python3.3.3]# python3.3 ez_setup.py  (Be sure to specify 3.3 of the execution file, otherwise use the system's default Python)
[root@localhost python3.3.3]# Easy_install

Now you can see the version

Easy_install easy_install-2.6 easy_install-3.3

Then the following installation is the same as the 2.6 step, but must specify a certain command (3.3 or 2.6) when installing.
After the installation is complete, the two versions complement each other and have their own virtual environment to execute the scripts within their environment.
The installation was successful, and the error follows the installation on the other server:

[Root@www python3.3]# python3.3 ez_setup.py extracting in/tmp/tmpj462kb traceback (most recent call last): File "Ez_se tup.py ", line 332, in <module> sys.exit (Main ()) File" ez_setup.py ", line 329, in Main return _install (archive, _build_install_args (options)) File "ez_setup.py", line Wuyi, in _install with Archive_context (archive_filename): File "/ usr/local/python3.3/lib/python3.3/contextlib.py ", line-in-__enter__ return to Next (Self.gen) File" ez_setup.py ", line Archive_context Archive.extractall () File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1232, in Extrac Tall self.extract (Zipinfo, Path, pwd) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1220, in extract RET Urn Self._extract_member (member, PATH, pwd) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1282, in _ Extract_member with Self.open (member, PWD=PWD) as source, \ File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1202, in open Close_fileobj=noT self._filepassed) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 649, in __init__ self._decompressor = _g Et_decompressor (Self._compress_type) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 612, in _get_  Decompressor return Zlib.decompressobj ( -15) attributeerror: ' Nonetype ' object has no attribute ' decompressobj ' [root@www 

 python3.3]#

Missing related modules:
Online is solved by the following way:

Yum install build-essential libssl-dev libxml2-dev libbz2-dev libjpeg62-dev libreadline5-dev WV poppler-utils ZLIB1G zlib 1g-dev zlibc Libghc6-zlib-dev ZLIBC

But I reinstalled it once Python ran again without error.

But there was a problem when installing virtualenv:

[Root@www python3.3]# easy_install-3.3 virtualenv
searching for virtualenv
Reading simple/virtualenv/
Download error on https://pypi.python.org/simple/virtualenv/: Unknown URL Type:https--Some Packages May is found!
Couldn ' t find index page for ' Virtualenv ' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on H ttps://pypi.python.org/simple/: Unknown URL Type:https--Some packages may isn't be found!
No Local Packages or download links found for virtualenv
error:could don't find suitable distribution for REQUIREMENT.P Arse (' virtualenv ')
[root@www python3.3]#

The path to the prompt (https://pypi.python.org/simple/virtualenv/) can be downloaded directly, or it may be installed:

[Root@www python3.3]# wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.tar.gz#md5= e03b76752b8ce7eee67c6298414cac79 [root@www python3.3]# ls bin ez_setup.py include Lib Setuptools-5.2.zip share
virtualenv-1.9.tar.gz [root@www python3.3]# easy_install-3.3 virtualenv-1.9.tar.gz Processing virtualenv-1.9.tar.gz Writing/tmp/easy_install-quwwll/virtualenv-1.9/setup.cfg Running virtualenv-1.9/setup.py-q Bdist_egg--dist-dir/ tmp/easy_install-quwwll/virtualenv-1.9/egg-dist-tmp-xhue8r warning:no previously-included files matching ' * ' found Under directory ' docs/_templates ' warning:no previously-included files matching ' * ' found under directory ' Docs/_build ' A Dding virtualenv 1.9 to easy-install.pth file installing virtualenv script To/usr/local/python3.3/bin Installing virtuale nv-3.3 Script To/usr/local/python3.3/bin installed/usr/local/python3.3/lib/python3.3/site-packages/ Virtualenv-1.9-py3.3.egg processing dependencies for virtualenv==1.9 finished processing DEpendencies for virtualenv==1.9 [root@www python3.3]# VI vi vigr virtualenv virtualenv-3.3 view VIPW

 virtualenv-2.6 Visudo

There was a problem installing flask:

Attributeerror: ' httpsconnection ' of ' module ' object has no attribute

In fact, the bottom line is that there is no normal Python installation, missing some of the modules caused, so the installation must pay attention to, if not installed well, then reload it.
Install all the development kits before you install Python

[Root@lujie ~]# yum groupinstall ' Development tools '

[Root@lujie ~] #yum install zlib-devel bzip2-devel openssl-devel Ncurses-devel

V. Summary of installation:

What is the difference between Easy_install virtualenv and pip install virtualenv?
The role of Easy_insall and Cpan in Perl, similar to gems in Ruby, provide an easy way for a fool to install an online one-click Module, while the PIP is an improved version of Easy_install, providing better hints and removal of package features. In the old version of Python, there was only Easy_install, no PIP.
Usage of Easy_install:

1) Install a package

$ easy_install <package_name>

$ easy_install "<package_name>==<version>"

2) Upgrade a package

$ easy_install-u "<package_name>>=<version>"

Usage of PIP

1) Install a package

$ pip Install <package_name>

$ pip Install <package_name>==<version>

2 Upgrade a package (if no version number is provided, upgrade to the latest version)

$ pip Install--upgrade <package_name>>=<version>

3) Delete a package

$ pip Uninstall <package_name>

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.