Flash framework learning notes installation (windows installation and centos installation)

Flask relies on two external libraries: Werkzeug and Jinja2. Werkzeug is a tool set of WSGI (standard Python interface developed and deployed between web applications and multiple servers), and Jinja2 is responsible for rendering templates.

1, Installation

Prerequisites for flash installation

1. Installed python2 X version

2. Easy is installed_ install

Before installing flash, you must first install Python and easy_install,easy_ Install only supports Python 2 Python 3.0 is not supported in version X X version, so you'd better choose python2 when installing python x. This is 2.7

python2. The installation of 7 is very simple. There are many articles in this station. There is no description here. The path is arbitrary. After the installation is completed, add environment variables.

win7 :

Configure environment variables

Method 1: (Computer - properties - advanced system settings - environment variables - add python installation path)

Method 2: cmd

Setting: set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

View: echo% path%

easy_install:

Download address: http://pypi.python.org/pypi/setuptools

The windows version will only have one ez_setup.py script, download it and put it in D: \ Python \ python2 7 directory, easy will be installed automatically_ Install, directory: D: \ Python \ python2 7\Scripts

easy_ Add environment variables to install: the path is D: \ Python \ python2 7\Scripts

After installing these two, start installing flash

Install virtualenv, which is mainly used to isolate the interpreter environment and avoid the library dependency of multiple pythons or multiple pythons on the same machine

Then configure the virtual environment

Then cd to the Scripts in the myvir directory

Enter activate Bat, enter the virtual environment, and then enter easy_ Install flash installation

Test results, whether the installation is successful:

In pycharm software, to create a flask project, select python. In myvir Exe to run the script.

Create a simple hello world script with the 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 to display

It can be accessed through the website given. Note: it is always running at this time. To close the port after completion, click Run - > stop in pycharm. By default, only local access is available, and the port is 5000

The last line is changed to app Run ('0.0.0.0 ', 12345), and others can access it

Resolution:

​
    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 program. This parameter is required so that flash can know where to find it

To templates and static files.

@app.route("/")

Use the route() decorator to tell flash the URL of the trigger function. It can be customized, such as @ app Route ("/ test1. Py"), followed by the file name when accessing

​
    def hello():
    
        return "Hello World!"

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

app.run()

Run the server application. After running, it is only accessible locally by default. If you need other connections, you can specify host, such as app run(host='0.0.0.0')

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

Externally accessible server

If you run the server, you will notice that it can only be accessed from your own computer, not anywhere else in the network. This is by default, because in debug mode, users can execute arbitrary Python code on your computer.

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

app.run(host='0.0.0.0')

This allows the operating system to listen to all public IP addresses.

2, Install version 3.3 under windows:

Note: if you have installed 2.7 by default and want to install 3.3, you must enter the 3.3 installation directory and run python ez_setup.py (ez_setup.py) download address: https://pypi.python.org/pypi/setuptools ).

Then enter the newly generated scripts directory and execute the following command to install virtualenv.

Similarly, 3.3 virtualenv should also be used when generating a virtual environment, otherwise an error will be reported.

Then cd to the Scripts in the myvir directory

Enter activate Bat, enter the virtual environment, and then enter easy_ Install flash installation

3, CentOS 6 4. Install python2 6 flash framework:

Installation execution command:

​
    yum install openssh-server
    python --version((see if it is a compliant version)
    yum install python-setuptools
    easy_install virtualenv(The system is installed by default easy_install2.6)
    virtualenv   
    

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

1. Global (not recommended):

easy_ Install flash global installation. Here are the methods of local installation.

Test code:

​
    from flask import Flask 
    app = Flask(__name__) 
    @app.route('/') 
    def hello_world(): 
    return "Hello World!" 
    if __name__ == '__main__': 
    app.run(host='0.0.0.0') 

There is no requirement for the location of documents, which can be implemented anywhere. (Global installation) access from another server: http://IP:5000 Other methods, such as template rendering, also only need to create templates folder in your project.

2. Local environment:

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

​
    [root@localhost opt]# mkdir myweb
    
    [root@localhost opt]#cd myweb
    
    [root@localhost myweb]# virtualenv venv (Note: the name of the venv directory here is set by yourself)
    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 corresponding environment.

Benefits:

Everything has been installed in this virtual environment, so your main Python installation environment will not be affected (you can support the use of several environments at the same time). An added benefit is that root administrator privileges are not required for installation in this way. After migration, the executable file will be unusable due to the change of path, or the environment needs to be rebuilt.

On OS X and Linux, do the following:

​
    [root@localhost venv]# . bin/activate   #Active (active for each run)
    (venv)[root@localhost venv]# 
    
    

The following actions apply to Windows:

$ venv\scripts\activate

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

Now you just need to type the following command to activate flash in virtualenv:

    (venv)[root@localhost venv]# pip install Flask             #Just start execution once
    ......
    Successfully installed Flask Werkzeug Jinja2 itsdangerous markupsafe
    Cleaning up...

In a few seconds, everything was done. Possible errors:

SSLError: The read operation timed out

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

If this error occurs, just re execute the command.

Execution activate.csh can exit virtualenv (I don't know if it's the right way, but it can exit)

4, Install Python 3.0 under centos 3 flash framework:

If easy in 3.3_ 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 the execution file of 3.3, otherwise the default python of the system will be used)
    [root@localhost python3.3.3]# easy_install

You can see the version after completing

easy_install easy_install-2.6 easy_install-3.3

Then, the following installation steps are the same as those in 2.6. Only when installing, you must specify a certain command (3.3 or 2.6). After the installation is completed, the two versions complement each other and have their own virtual environment to execute the scripts in their own environment. The previous installation is successful, but the following installation on another server will report an error:

    [root@www python3.3]# python3.3 ez_setup.py 
    Extracting in /tmp/tmpj462kb
    Traceback (most recent call last):
     File "ez_setup.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 51, in _install
      with archive_context(archive_filename):
     File "/usr/local/python3.3/lib/python3.3/contextlib.py", line 48, in __enter__
      return next(self.gen)
     File "ez_setup.py", line 101, in archive_context
      archive.extractall()
     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, 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 = _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]# 
    
    

Lack of relevant modules: online is solved in the following ways:

    yum install build-essential libssl-dev libxml2-dev libbz2-dev libjpeg62-dev libreadline5-dev wv poppler-utils zlib1g zlib1g-dev zlibc libghc6-zlib-dev zlibc
    
    

But I reinstalled python once and ran it again without error.

But there was a problem when installing virtualenv:

    [root@www python3.3]# easy_install-3.3 virtualenv
    Searching for virtualenv
    Reading https://pypi.python.org/simple/virtualenv/
    Download error on https://pypi.python.org/simple/virtualenv/: unknown url type: https -- Some packages may not be 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 https://pypi.python.org/simple/: unknown url type: https -- Some packages may not be found!
    No local packages or download links found for virtualenv
    error: Could not find suitable distribution for Requirement.parse('virtualenv')
    [root@www python3.3]#
    
    

Then find the path of the prompt directly( https://pypi.python.org/simple/virtualenv/ )Download or 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]# 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'
    Adding virtualenv 1.9 to easy-install.pth file
    Installing virtualenv script to /usr/local/python3.3/bin
    Installing virtualenv-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: 'module' object has no attribute 'HTTPSConnection'

In fact, in the final analysis, Python is not installed normally and some modules are missing, so you must pay attention to it when installing. If it is not installed properly, reinstall it. Install all development kits before installing python

    [root@lujie ~]# yum groupinstall "Development tools"
    
    [root@lujie ~]#yum install zlib-devel bzip2-devel openssl-devel ncurses-devel
    
    

5, Summary installation:

easy_ What is the difference between install virtualenv and pip install virtualenv? easy_insall is similar to cpan in perl and gem in ruby. It provides a convenient way to install modules online with one click, while pip is easy_ The improved version of install provides better prompt information and functions such as deleting package. In the old version of python, there was only easy_install, no pip. easy_ Usage of 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 the version number is not provided, upgrade to the latest version)

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

3) Delete a package

    $ pip uninstall <package_name>
    

 

Keywords: Python

Added by Mordecai on Sun, 30 Jan 2022 08:51:00 +0200