path to modules per import statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AndyL

    #1

    path to modules per import statement

    Hi,
    is there any way to specify the path to modules within import statement
    (like in Java)?

    For instance: "import my.path.module" would load module from
    ../my/path/module.py?

    Thx,
    A.
  • danmcleran@yahoo.com

    #2
    Re: path to modules per import statement

    >For instance: "import my.path.module" would load module from[color=blue]
    >./my/path/module.py?[/color]

    Yeah, just do that. I don't understand the question, it works just like
    this today.

    Comment

    • danmcleran@yahoo.com

      #3
      Re: path to modules per import statement

      As an example, let's say you have a main module at /usr/code/Main.py
      and you have a module you'd like to import at /usr/code/util/Util.py,
      you can do this:

      import util.Util

      If you are using PyDev and Eclipse to develop your Python code, you can
      set the base directory to reference module imports from.

      You can also tweak your PYTHONPATH if you want to import modules from
      other directory structures.

      Comment

      • Arne Ludwig

        #4
        Re: path to modules per import statement

        Maybe he means: sys.path.append ('/my/path')

        Comment

        • AndyL

          #5
          Re: path to modules per import statement

          danmcleran@yaho o.com wrote:[color=blue][color=green]
          >>For instance: "import my.path.module" would load module from
          >>./my/path/module.py?[/color]
          >
          >
          > Yeah, just do that. I don't understand the question, it works just like
          > this today.
          >[/color]

          I work on rather big set of Python applications: something like 100 .py
          files divided into libs and separate sub-applications.

          For now I keep almost everything in one directory but I wish following
          structure to be in place:

          app1/ app2/ lib1/ lib2/ lib3/


          and be able to import from each app[12] all the libs. I do not want to
          touch existing code to prefix all the import places with lib[123] nether
          I want to play with sys.path.append too much.


          A.

          Comment

          • Fuzzyman

            #6
            Re: path to modules per import statement


            AndyL wrote:[color=blue]
            > danmcleran@yaho o.com wrote:[color=green][color=darkred]
            > >>For instance: "import my.path.module" would load module from
            > >>./my/path/module.py?[/color]
            > >
            > >
            > > Yeah, just do that. I don't understand the question, it works just like
            > > this today.
            > >[/color]
            >
            > I work on rather big set of Python applications: something like 100 .py
            > files divided into libs and separate sub-applications.
            >
            > For now I keep almost everything in one directory but I wish following
            > structure to be in place:
            >
            > app1/ app2/ lib1/ lib2/ lib3/
            >[/color]

            There are several approaches.

            One is to add each of these directories to your sys.path :

            sys.path.extend (map(os.path.ab spath, ['app1/', 'app2/', 'lib1/',
            'lib2/', 'lib3/']))

            This is a perfectly normal thing to do - so I wouldn't be shy of it.

            Another is to add an empty file called ``__init__.py`` to each of these
            directories. This makes each directory a 'package'. You can then do :

            import app1.module

            My module `pathutils
            <http://www.voidspace.o rg.uk/python/pathutils.html> `_ contains a third
            approach that *does* allow you to specify the location of your module.
            It's a less suitable approach in your case however.

            HTH

            Fuzzyman
            http://www.voidspace.org.uk/python/index.shtml
            [color=blue]
            >
            > and be able to import from each app[12] all the libs. I do not want to
            > touch existing code to prefix all the import places with lib[123] nether
            > I want to play with sys.path.append too much.
            >
            >
            > A.[/color]

            Comment

            • Peter Hansen

              #7
              Re: path to modules per import statement

              AndyL wrote:[color=blue]
              > danmcleran@yaho o.com wrote:
              > I work on rather big set of Python applications: something like 100 .py
              > files divided into libs and separate sub-applications.
              >
              > For now I keep almost everything in one directory but I wish following
              > structure to be in place:
              >
              > app1/ app2/ lib1/ lib2/ lib3/
              >
              >
              > and be able to import from each app[12] all the libs. I do not want to
              > touch existing code to prefix all the import places with lib[123] nether
              > I want to play with sys.path.append too much.[/color]

              You should use Fuzzyman's solution of __init__.py files (look in the
              Python documentation for information about "packages". .. I believe it's
              even covered adequately in the tutorial, so maybe rereading that would
              help), but also look into the use of ".pth" files. This is documented
              in the online help for the "site" standard library module, or in the
              helpful comments in the source in python's lib/site.py file.

              Basically to find the packages in lib1/ and friends regardless of what
              the current directory is, you need to use either the PYTHONPATH
              environment variable, modify sys.path directly (and it is acceptable to
              do that in various circumstances), or use .pth files as documented to
              get the desired effect.

              -Peter

              Comment

              Working...