Creating Python Modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam T. Gautier

    Creating Python Modules

    Greetings,

    I am trying to create a python module for a series of class files that
    because of their size I have divided into multiple files.

    class Foo --> foo.py
    class Boo --> boo.py

    I have created a setup.py where I have for the "py_modules =['foo',
    'boo']". when I run "%>./setup.py install" it copies all *.py and *.pyc
    to /usr/lib/python2.2/site-packages.

    When I goto use the module I have to use:

    import foo
    import boo

    I would like if anyone could help me to be able to:
    1.) Get the *.py and *.pyc (foo and boo) generated with the install to
    install to "/usr/lib/python2.2/site-packages/mymodule".
    2.) When using the module be able to use just one import and get source
    code like:

    import mymodule

    foo = Foo()
    boo = Boo()

    I have read the tutorial docs on this and it has brought me to where I
    am now but I have about 35 files/classes. I think that I need to define
    a mymodule.py file and put an __init__()? method into it I am just not
    sure what to do, so any help would be appreciated.


  • DH

    #2
    Re: Creating Python Modules

    Adam T. Gautier wrote:
    [color=blue]
    > Greetings,
    >
    > I am trying to create a python module for a series of class files that
    > because of their size I have divided into multiple files.
    >
    > class Foo --> foo.py
    > class Boo --> boo.py[/color]

    Create an __init__.py file in your module's root directory that calls:
    from foo import Foo
    from boo import Boo

    Then to use your module, a user would do:

    from mymodule import Foo, Boo
    foo = Foo()
    boo = Boo()

    or...

    import mymodule
    foo = mymodule.Foo()
    boo = mymodule.Boo()

    Comment

    • Dave Kuhlman

      #3
      Re: Creating Python Modules

      Adam T. Gautier wrote:
      [color=blue]
      > Greetings,
      >
      > I am trying to create a python module for a series of class files
      > that because of their size I have divided into multiple files.
      >
      > class Foo --> foo.py
      > class Boo --> boo.py
      >
      > I have created a setup.py where I have for the "py_modules =['foo',
      > 'boo']". when I run "%>./setup.py install" it copies all *.py and
      > *.pyc to /usr/lib/python2.2/site-packages.
      >
      > When I goto use the module I have to use:
      >
      > import foo
      > import boo
      >
      > I would like if anyone could help me to be able to:
      > 1.) Get the *.py and *.pyc (foo and boo) generated with the
      > install to install to "/usr/lib/python2.2/site-packages/mymodule".[/color]

      I think you want to include the "packages" option or the
      "package_di r" option in your setup.py.

      See section "3.1 Listing whole packages" in "Distributi ng Python
      Modules" at:

      The official home of the Python Programming Language



      Dave

      --

      dkuhlman@rexx.c om

      Comment

      Working...