How to import a third-party module in Python?

I found a third-party module that I would like to use. How can I technically import this module?

In particular, I want to use a module called context_manager . obviously i can't just import garlicsim.general_misc.context_manager because it will not find garlicsim . So what should I write to import a thing?

EDIT: I am using Python 3.x and Python 2.x, and I would like to receive answers regarding both versions.

+6
python import module
source share
3 answers

In the case of garlicsim, you want to install it after the garlicsim instruction . You can also download the code and run python setup.py install in the right directory for this and almost any other library.

One note, since you may be new to python, this is the python 3 library. If you use python 2 (most likely if you don't know), this will not work correctly. You will want to install python 2 version.

+6
source share

You need to install the module somewhere in PYTHONPATH. For almost all python modules, you can use easy_install or your own setup.py script for the package.

+1
source share

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.

Using

According to the Python style guide :

Import is always placed at the top of the file, immediately after any module comments and docstrings, as well as before modular globals and constants.

Import should be grouped in the following order:

  • import standard library
  • Third Party Related Imports
  • import local applications / libraries

You must put an empty line between each import group.

 >>> 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 already in Python 3.2 :

class contextlib.ContextDecorator - the base class that allows the context manager, which will also be used as a decorator.

And the 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> 
0
source share

All Articles