Skip to content
Search

Python:Module

lastmod 2022-05-26created 2015-04-145 edits

Importing packages

To run the echo_test function in /home/user/python/game/sound/echo.py, add the /home/user/python/ directory to the PYTHONPATH environment variable:

PYTHONPATH=/home/user/python/:$PYTHONPATH

First method

You can import the echo module and run it as follows.

>>> import game.sound.echo
>>> game.sound.echo.echo_test()
echo

Second method

You can use from ... import down to the directory containing the echo module and run it as follows.

>>> from game.sound import echo
>>> echo.echo_test()
echo

Third method

You can import the echo_test function of the echo module directly and run it as follows.

>>> from game.sound.echo import echo_test
>>> echo_test()
echo

Importing with relative paths

When using <cdoe>.</code>, you need to put one space after it.

from . file import ClassName

import path

When using import, an error message similar to the one below may be printed.

Traceback (most recent call last):
  File "/usr/local/bin/scons", line 188, in <module>
    import SCons.Script
ImportError: No module named SCons.Script

This happens because Python cannot read the import path list; add the path to the PYTHONPATH environment variable.

export PYTHONPATH=/Library/Python/2.7/site-packages/scons-2.3.0/:

Favorite site

author Zer0 sitemap