I've found a third party module which I would like to use. How do I technically import that module?
Particularly, I want to use a module called context_manager. obviously, I cannot just import garlicsim.general_misc.context_manager
because it won't find garli开发者_高级运维csim
. So what should I write to import the thing?
EDIT: I'm using both Python 3.x and Python 2.x and I'd like to get answers relevant to both versions.
In garlicsim's case you'd want to install it following garlicsim's installation instructions. You could also download the code and in the right directory run python setup.py install
for this and almost any other library.
One note, since you may be new to python, is that that's a python 3 library. If you're using python 2 (more likely if you don't know) it won't work right. You'll want to install the python 2 version.
You need to install the module somewhere in your PYTHONPATH. For nearly all python modules, you can use easy_install
or the package's own setup.py
script to do this for you.
Installation
GarlicSim is dead but still available:
C:\Python27\Scripts>pip search garlicsim
garlicsim_lib - Collection of GarlicSim simulation packages
garlicsim_lib_py3 - Collection of GarlicSim simulation packages
garlicsim_wx - GUI for garlicsim, a Pythonic framework for
computer simulations
garlicsim - Pythonic framework for working with simulations
garlicsim_py3 - Pythonic framework for working with simulations
Use pip install garlicsim
to install it.
Usage
According to the Python style guide:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
- standard library imports
- related third party imports
- local application/library specific imports
You should put a blank line between each group of imports.
>>> import garlicsim.general_misc.context_manager as CM
>>> help(CM)
Help on module garlicsim.general_misc.context_manager in garlicsim.general_misc:
NAME
garlicsim.general_misc.context_manager - Defines the `ContextManager` and `ContextManagerType` classes.
FILE
c:\python27\lib\site-packages\garlicsim\general_misc\context_manager.py
DESCRIPTION
Using these classes to define context managers allows using such context
managers as decorators (in addition to their normal use) and supports writing
context managers in a new form called `manage_context`. (As well as the
original forms).
[...]
>>> from garlicsim.general_misc.context_manager import ContextManager
>>> help(ContextManager)
Help on class ContextManager in module garlicsim.general_misc.context_manager:
class ContextManager(__builtin__.object)
| Allows running preparation code before a given suite and cleanup after.
Alternative
It looks like that's already in Python 3.2:
class contextlib.ContextDecorator - A base class that enables a context manager to also be used as a decorator.
And contextmanager is as old as Python 2.5:
from contextlib import contextmanager
@contextmanager
def tag(name):
print "<%s>" % name
yield
print "</%s>" % name
>>> with tag("h1"):
... print "foo"
...
<h1>
foo
</h1>
精彩评论