path to modules per import statement

A

AndyL

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.
 
D

danmcleran

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

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

danmcleran

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.
 
A

AndyL

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

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.
 
F

Fuzzyman

AndyL said:
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/

There are several approaches.

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

sys.path.extend(map(os.path.abspath, ['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.org.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
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.
 
P

Peter Hansen

AndyL said:
(e-mail address removed) 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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,134
Latest member
JavierIlif

Latest Threads

Top