Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, August 6, 2012

python virtualenv

There are already lots of articles telling us why we need virtualenv to provide a separate environment(sandbox), such as different projects may depends on different libraries, or you don't have the write permission to the /path/to/python/site-packages directory.

I'm using pip to install virtualenv, it's pretty easy,
pip install virutalenv

then create an sandbox for my project, with the option --no-site-packages to isolate from main site-packages,
virutalenv --no-site-packages social-map 

finally, i cloned my project directly into social-map. To active this environment,
source social-map/bin/active

if you want to deactive this environment, just type deactive. I don't know how to deactive the envrionment before, what i did was exit the terminator, then open a new one, very silly...

we can check the settings as `which python` etc. If you check the active script in ~/your/env/bin/, you can find this, virtual will change your path, and put the virtualenv's path at the beginning of the path, it will change your terminer's PS1 also.
VIRTUAL_ENV="/Users/pengphy/Codes/virtualenvs/social-map"
PATH="$VIRTUAL_ENV/bin:$PATH"

http://www.doughellmann.com/articles/pythonmagazine/completely-different/2008-02-ipython-and-virtualenv/index.html 


http://mitchfournier.com/2010/06/25/getting-started-with-virtualenv-isolated-python-environments/

Friday, July 13, 2012

import

import可以有一下几种形式:
  • import A (推荐都使用这种方式)
        将A导入到当前的namespace
        变种可以时 import A.B.C.D 如果是这种情况 ABC必须是Package 而D只能是一个模块 或者时一个Pakcage

  • from A import B
        将B导入到当前的namespace 当改变B时 对B作出的修改不会影响到A.B

  • X = __import__('X') (运行时才知道X的名称)

  • from A import *
        将A中所有的public name引入到当前的namespace public name在这里指的是name明称不以_(下划线)开头的那些name


import vs loading

一个模块不管被imoprt多少次 它只会被load一次 如果有执行代码在这个模块里面 那么只有第一次被加载的时候会运行 之后import则不会去运行未包装的执行代码



What does python do to import a Module?
1, check sys.modules to see if module is already imported, if that's the case, python will use the existing module object as is.
2, otherwise, create a new , empty module object
3, insert that module in sys.modules
4, Load the module code object
5, Execute the module code object in the new module's namespace. All variables assigned by the code will be available via the module object.


Importing * From a Package

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package.


If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements. Consider this code:
import sound.effects.echo
import sound.effects.surround
from sound.effects import *
In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from...import statement is executed. (This also works when __all__ is defined.)