If I have /home/doriad/Scripts/Python/
and inside I have a bunch of folders, ie Geometry, Math, Other, etc that each contain scripts (.py files), is there a way I can just add /home/doriad/Scripts/Python to PYTHONPATH and then somehow

from Geometry/Spherical import *

rather than having to add every subdirectory to the PYTHONPATH explicitly?

Thanks,
Dave

If you want to import custom module xyq via the Pythonpath
create a file xyq.pth and stick it into a directory python
will look into, like
for Windows:
C:\Python25\Lib\site-packages
for Linux:
/usr/lib/python2.5/site-packages

The .pth file contains a line showing the directory
in which module xyq is located, for instance:
for Windows:
C:\Python25\Mymodules
for Linux in your case:
/home/doriad/Scripts/Python/

sneekula - you'd still have to put a line for every single subdirectory though, right?

What I'm saying is I'd just want to add "/home/doriad/PythonScripts" to my PYTHONPATH and then any subfolders of PythonScripts will be automatically searched - the idea being that if I rearrange the contents of PythonScripts and change the names of subfolders, etc I will not have to update my PYTHONPATH.

Does that make sense?

Dave

How about you use __init__.py in each of your subdirectories? Here's an illustration of the __init__.py idea from an old mailing list:

__init__.py is used for two things. One is as a flag to
indicate that the python programs in the directory are
part of a module.

The other is as the module itself. Let's take a simple
example. Assume you have a directory named breakfast
which contains modules named spam.py, eggs.py,
toast.py and jam.py, and that the directory containing
breakfast is on the PYTHONPATH.

If it try to import spam.py by writing

import breakfast.spam

it won't work because the breakfast directory
doesn't contain an __init__.py file.

If I then add __init__.py to the breakfast directory,
the import will work, and the result will be *two*
modules loaded. The first module will be bound to
the identifier 'breakfast' in your module. It will be
completely empty except for the identifier 'spam'
which will have the spam module bound to it.

This means that if the spam module contains,
for example, an identifier named "vikings", then I
can refer to it as breakfast.spam.vikings.

The real kicker here is that when I say that the
first module will be completely empty, it's not
quite true. First, it will have some standard
identifiers that all modules have, and second
it will have anything you put into the __init__.py
module file. This last is an advanced feature, and
you're well advised to stay away from it until
you've got a lot more experiance.

HTH

John Roth

[ Source ]

If I have /home/doriad/Scripts/Python/
and inside I have a bunch of folders, ie Geometry, Math, Other, etc that each contain scripts (.py files), is there a way I can just add /home/doriad/Scripts/Python to PYTHONPATH and then somehow

from Geometry/Spherical import *

rather than having to add every subdirectory to the PYTHONPATH explicitly?

Thanks,
Dave

I agree with jml699, put an empty file __init__.py in the Geometry folder and then use

from Geometry.Spherical import *

I read somewhere that the file __init__.py is no longer necessary in python 3.0.

Sorry for being so slow! Here it goes, I hope you can follow me on Linux:
Create a directory let's say 'MyModules' in a path that Python normally looks at. Then create a module and file it in the MyModules directory...

# save this as module1.py  (file/module name is case sensitive)
# for instance in directory D:\Python25\MyModules

text = "text from module1"

First attempt to import modul1 fails, since Python has not been given the MyModules directory name and cannot find the module in it ...

# import module module1 located in
# D:\Python25\MyModules

import module1

print(module1.text)

"""
my output -->
ImportError: No module named module1
"""

Second attempt still fails. Python goes to the MyModules directory level only ...

# import module module1 located in
# D:\Python25\MyModules
# without a __init__.py file

import MyModules.module1 as my_module1

print(my_module1.text)

"""
my output -->
ImportError: No module named MyModules.module1
"""

Finally success, Python goes to the directory and finds the __init__.py file, assumes its a package name and looks further ...

# import module module1 located in
# D:\Python25\MyModules
# after creating a file named __init__.py in D:\Python25\MyModules
"""
__init__.py is just an 'empty' file containing for instance a 
# comment  line to explain its purpose
"""

import MyModules.module1 as my_module1

print(my_module1.text)

"""
my output -->
text from module1
"""

Python3 still uses __init__.py files, as a short test showed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.