Flask Framework Learning Note (i) Installation (Windows installation and CentOS installation)

Source: Internet
Author: User
Tags virtual environment virtualenv
Flask relies on two external libraries: Werkzeug and JINJA2. Werkzeug is a toolset for WSGI (a standard Python interface developed and deployed between Web applications and multiple servers), JINJA2 is responsible for rendering the template.

First, installation

Prerequisites for flask Installation

1. The python2.x version is installed

2. Easy_install installed

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

python2.7 installation is very simple, this site has a lot of articles introduced, not described here, the path arbitrary, install the completion of the environment variable can be added.

Win7:

Configuring Environment variables

Method One: (Computer-"Properties-" advanced system settings--"Environment variables--" Add Python installation path)

Method Two: Under CMD

Setting: 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 in 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 these two and then start installing 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 CD to the Myvir directory under scripts

Enter Activate.bat, enter the virtual environment, then enter Easy_install flask Install

Test results, whether the installation was successful:

In Pycharm software, to create a flask project, and then select Python.exe in Myvir to run the script.

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

From flask import Flaskapp=flask (__name__) @app. Route ('/') def hello_world (): Return "Hello world~~~" if __name__ = = ' __ Main__ ': App.run ()

Then click Run to display the

can be accessed through the given URL. Note: At this point, you are running, and when you are finished, close the port by clicking Run->stop in Pycharm.
By default, only local access is available and the port is 5000.

The last line is modified to App.run (' 0.0.0.0 ', 12345), allowing others to 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, here __name__ refers to the __main__ main program. This parameter is required so that Flask can know where to look for

To templates and static files and things like that.

@app. Route ("/")

Use the route () adorner to tell the URL of the Flask trigger function. Can be customized, such as @app. Route ("/test1.py"), when access is followed by the file name

def hello ():    return "Hello world!"

Defines the function that is used to generate the associated URL and returns information that needs to be displayed in the user's browser.

App.run ()

Run the server application, after running the default is only locally accessible, if you want to make other connections, you can specify host, such as: App.run (host= ' 0.0.0.0 ')

The default port used is: 5000, you can use the 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 not anywhere else on the network. This is the default, because in debug mode, users can execute arbitrary Python code on your computer.

If you disable debug or trust the user of your network, you can simply modify the method that calls run () to make your server publicly available, as follows:

App.run (host= ' 0.0.0.0 ')

This will allow the operating system to listen on all public IPs.

Second, the installation of Windows 3.3 version:

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 execute the following command to install VIRTUALENV.

Similarly, the creation of the virtual environment should also use 3.3 virtualenv, otherwise error.

Then CD to the Myvir directory under scripts

Enter Activate.bat, enter the virtual environment, then enter Easy_install flask Install

Third, centos6.4 installation python2.6 flask frame::

Install the Execute command:


After installation, you can immediately open the shell and create your own environment.

1. Global (not recommended):

Easy_install Flask Global installation, the following is a partial installation method.

Test code:

The location of the file is not required and can be performed anywhere. (Global installation)
Access from another server: http://IP:5000 way
Then other, such as template rendering, will only be implemented in the same way that you create the Templates folder in your project.

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/pythoninstalling setuptools, Pip...done.

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

Benefits:

Everything is already installed in this virtual environment, so your own primary Python installation environment is unaffected (it can support several environments at the same time). An additional benefit is that the root administrator privilege is not required to install this way.
After the migration, the execution of the file will not be used because of changes to the path, or the environment needs to be rebuilt.

On OS X and Linux, do the following:

[Root@localhost venv]#. Bin/activate   

The following actions apply to Windows:

$ venv\scripts\activate

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

Now you just 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 markupsafecleaning up ...

After a few seconds, everything was done.
Errors may occur:

Sslerror:the read operation timed out

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

The wrong thing to do is to just re-execute the command.

Perform. Activate.csh can exit virtualenv (do not know if it is the right way, but can exit)

Iv. flask Framework for installing python3.3 under CentOS:

If the 3.3 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 the system's default python will be used) [Root@localhost python3.3.3]# Easy_install

At this point, you can see the version

Easy_install easy_install-2.6 easy_install-3.3

Then the subsequent installation is the same as the 2.6 step, only the installation must specify the specified command (3.3 or 2.6)
After the installation is complete, both versions are complementary and have their own virtual environment to execute scripts within their own environment.
The previous installation was successful and the subsequent installation on another server was an error:

[Root@www python3.3]# python3.3 ez_setup.py extracting In/tmp/tmpj462kbtraceback (most recent call last): File "Ez_setup. Py ", line 332, in
 
  
  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 __enter__," Return to Next "(Self.gen) File" ez_setup.py ", line 101, in Archive_context Archive.ext Ractall () File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1232, in Extractall self.extract (zipinfo, Path, PWD) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1220, in Extract return 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, PW D=PWD) as source, \ File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 1202, in open Close_fileobj=not self._file Passed) File "/usr/local/python3.3/lib/python3.3/zipfile.py", line 649, in __init__ self._decompressor = _get_ 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 re-installed the Python once again to run without error.

But there's something wrong with installing virtualenv:

[Root@www python3.3]# easy_install-3.3 virtualenvsearching for virtualenvreading https://pypi.python.org/simple/ Virtualenv/download error on https://pypi.python.org/simple/virtualenv/: Unknown URL Type:https--Some packages Be found! Couldn ' t find index page for ' Virtualenv ' (maybe misspelled?) Scanning index of all packages (this could take a while) Reading https://pypi.python.org/simple/Download error on HTTPS://PYP i.python.org/simple/: Unknown URL Type:https--Some packages May is not found! No Local Packages or download links found for virtualenverror:could don't find suitable distribution for requirement.parse ( ' Virtualenv ') [Root@www python3.3]#

Then directly find the path to the prompt (https://pypi.python.org/simple/virtualenv/) to download, or you can install:

[Root@www python3.3]# wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.tar.gz#md5= E03b76752b8ce7eee67c6298414cac79[root@www python3.3]# lsbin 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.gzprocessing Virtualenv-1.9.tar.gzwriting/tmp/easy_install-quwwll/virtualenv-1.9/setup.cfgrunning Virtualenv-1.9/setup.py-q Bdist_egg--dist-dir/tmp/easy_install-quwwll/virtualenv-1.9/egg-dist-tmp-xhue8rwarning:no previously-included Files matching ' * ' found under directory ' docs/_templates ' warning:no previously-included files matching ' * ' found under D Irectory ' Docs/_build ' Adding virtualenv 1.9 to easy-install.pth fileinstalling virtualenv script to/usr/local/python3.3 /bininstalling virtualenv-3.3 Script to/usr/local/python3.3/bininstalled/usr/local/python3.3/lib/python3.3/ site-packages/virtualenv-1.9-py3.3.eggprocessing dependencies for virtualenv==1.9finished processing dependencies For Virtualenv==1.9[root@www python3.3]# vivi vigr virtualenv virtualenv-3.3view vipw virtualenv-2.6 V Isudo

The problem occurs when installing flask:

Attributeerror: ' Module ' object has no attribute ' httpsconnection '

In fact, in the final analysis are not normal installation of Python, missing some of the modules caused, so when the installation must pay attention, if not installed, then reinstall 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 ncurs Es-devel

V. Summary of the installation:

Easy_install virtualenv and Pip install virtualenv the difference?
Easy_insall's role is similar to the CPAN in Perl, Ruby's Gem, and offers a fool-friendly way to install modules online, while Pip is an improved version of Easy_install, providing better tips and removing features like package. The old version of Python has only easy_install and no PIP.
Usage of Easy_install:

1) Install a package

$ easy_install 
 
  
   
  $ easy_install "
  
   
    
   = =
   
    
     
    "
   
    
  
   
 
  

2) Upgrade a package

$ easy_install-u "
 
  
   
  >=
  
   
    
   "
  
   
 
  

Usage of PIP

1) Install a package

$ pip Install 
 
  
   
  $ pip Install 
  
   
    
   =
   
    
  =
   
 
  

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

$ pip Install--upgrade 
 
  
   
  >=
  
   
 
  

3) Delete a package

$ pip Uninstall 
 
  
  • 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.