Thursday, 6 September 2012

Frequently Asked Questions Python

How to submit a Python application

After development and Niece, Baidu open cloud support BAE Python runtime environment has been on the line, currently in beta.
If you want to develop Python applications and have the relevant experience in the development, please send an e-mail request to dev_support@baidu.com . When sending a message, please provide your Python Products developed and illustrative purposes. We will selectively opened for you Python runtime environment.

Chinese garbage problem

WEB development using python for the time, probably will encounter three forms of coding, namely "python script file stored on disk encoding used", "python script file loaded when explaining the encoding used," " browser displays Web content encoding used. "
Among them, the first coding and developers are using the editor related to the latter two can be achieved through python script file to control; if the first coding and the latter two are inconsistent, it may be garbled or wrong. As long as three encoding the same, you can properly display Chinese.
The following table is a variety of situations.
py files stored on disk encoding py py file to load the interpreter code Browser displays the coding Result
gbk gbk gbk Normal display
gbk gb2312 gb2312 Normal display
gbk utf8 utf8 Garbled
gb2312 gb2312 gb2312 Normal display
gb2312 gbk gbk Normal display
gb2312 utf8 utf8 Garbled
utf8 gbk gbk Garbled
utf8 gb2312 gb2312 Decoding fails, an exception is thrown
utf8 utf8 utf8 Normal display

The use virtualenv

python virtualenv to create an isolated environment, it can handle multiple versions and modules python dependency issue. See http://www.virtualenv.org/en/latest/index.html install virtualenv
• 1. Using a package manager install pip or easy_install
• 2. Download Source packets through setup.py install 
• 3. Download virtualenv.py direct execution
Create an isolated environment
  # # # Is recommended to add options - no-site-packages (isolated environment so that the system can not access the global site-packages directory)   ~ $ Virtualenv - no-site-packages ENV    # # # Or   ~ $ Python virtualenv. Py - no-site-packages ENV     ~ $ Source ENV / bin / activate   (ENV) ~ $ 
Use virtualenv Processing Dependency
Command-line prompt appears before a (ENV) prefix, indicating that already in the python isolated environment. Here we have an example of how to deal with in this virtual environment, the environment of third-party libraries python dependency problem.
For example, a BAE python application called appA, it relies on a third-party library pyX. First you need to take appA code via SVN locally, assuming the checkout to a local directory / home / duapp / dir_appA, and in that directory create a directory stored dependent module deps.
Then, in (ENV) in a virtual environment to download and install pyX, because virtualenv pip has been included in the package manager, we have to deal directly with pip install. Then copy the package already installed pyX deps next to the application directory.
 (ENV) ~ $ pip install pyX (ENV) ~ $ cd ENV / lib / pythonX. X / site-packages (ENV) ~ $ cp-r pyX / / home / duapp / dir_appA / deps / 
Now modify index.py file, add the module search path sys.path dependent module directory, as follows:
  import sys, os. path   deps_path = os. path. join (os. path. split (os. path. realpath (__ file__)) [0], 'deps')   sys. path. insert (0, deps_path) 
At this point, pyX has been successfully imported into appA, the application can use all the features of the third-party libraries, dependency problem is resolved.

How to distinguish between local development environment or BAE environment

Currently available through 'SERVER_SOFTWARE' environment variable to determine, at BAE environment, we define SERVER_SOFTWARE environment variable. Judgment method
 import os if 'SERVER_SOFTWARE' in os. environ: print "This is BAE environ" else: print "This is local environ" 

Database Import Failed

Through the cloud database phpmyadmin import mysql dump out of the statement, suggesting # 1044 - Access denied for user 'xxxxxxxxxxx' @ 'xxxxxxxxxxx' to database 'xxxxxxxxxxx'; This may be due to dump out the statement contains some users do not have permission execute statements such as LOCK, UNLOCK, etc.:
  LOCK TABLES `auth_group` WRITE;   / *! 40000 ALTER TABLE `auth_group` DISABLE KEYS * /;   / *! 40000 ALTER TABLE `auth_group` ENABLE KEYS * /;   UNLOCK TABLES; 
These statements will be removed, you can import a success

Imaging module import fails

By way of introduction as BAE preinstalled Imaging Module:
  from PIL import Imaging 

0 comments:

Post a Comment