how I import package

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simpleman
    New Member
    • Mar 2007
    • 1

    #1

    how I import package

    hi,
    I have two folders A,B
    A have foo.py
    B have new.py
    I want to import foo.py from A folder to new.py(it's in B folder).How can I do? But I didn't want to add foo.py in python's path.

    Is there any ways?

    --
    simpleman
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by simpleman
    hi,
    I have two folders A,B
    A have foo.py
    B have new.py
    I want to import foo.py from A folder to new.py(it's in B folder).How can I do? But I didn't want to add foo.py in python's path.

    Is there any ways?

    --
    simpleman
    I've been playing with
    Code:
    __import__(moduleName)
    which takes a string, but can't make it accept a full path name yet.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      I've been playing with
      Code:
      __import__(moduleName)
      which takes a string, but can't make it accept a full path name yet.
      Also, from the imp library module:
      3.21 imp -- Access the import internals

      This module provides an interface to the mechanisms used to implement the import statement. It defines the following constants and functions:
      find_module( name[, path])

      Try to find the module name on the search path path. If path is a list of directory names, each directory is searched for files with any of the suffixes returned by get_suffixes() above. Invalid names in the list are silently ignored (but all list items must be strings). If path is omitted or None, the list of directory names given by sys.path is searched, but first it searches a few special places: it tries to find a built-in module with the given name (C_BUILTIN), then a frozen module (PY_FROZEN), and on some systems some other places are looked in as well (on the Mac, it looks for a resource (PY_RESOURCE); on Windows, it looks in the registry which may point to a specific file).
      If search is successful, the return value is a triple (file, pathname, description) where file is an open file object positioned at the beginning, pathname is the pathname of the file found, and description is a triple as contained in the list returned by get_suffixes() describing the kind of module found. If the module does not live in a file, the returned file is None, filename is the empty string, and the description tuple contains empty strings for its suffix and mode; the module type is as indicate in parentheses above. If the search is unsuccessful, ImportError is raised. Other exceptions indicate problems with the arguments or environment.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        I've been playing with
        Code:
        __import__(moduleName)
        which takes a string, but can't make it accept a full path name yet.
        Of course, you can always do something like:
        Code:
        import sys
        
        sys.path.append(r"D:\My Documents\HETAP Project\2.05a")

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bartonc
          Of course, you can always do something like:
          Code:
          import sys
          
          sys.path.append(r"D:\My Documents\HETAP Project\2.05a")
          Another way to go is to put a file with the '.pth' extension in either the current directory or sitepackages (or other automatically searched directory) containing the path to the module/package in question.

          Comment

          Working...