For just running another python script from within python, there's a
number of options:
+ the exec statement: >>> help('exec')
+ the execfile() function (deprecated in py3k)
+ the imp module
On 3/24/08, Chris Hoeppner <[EMAIL PROTECTED]> wrote:
>
> Just thinking about this...
>
> Is there really no way to import something not being on the pythonpath?
> Maybe importing the file's contents as string and stuff it through some
> parser function (just ranting here, don't even know if there is such a
> thing)?
>
> What I want is make a few files execute in a certain order, and if I can
> do that without messing too much with sys.path, even better. Some of
> these files might even have the same name, so `import module` might not
> give me all the files I want either.
>
> Maybe the following workflow would make it possible, even though it
> feels *really* hacky:
>
> pathbk = sys.path
> paths = ['/path/1/file1.py', '/path/2/file2.py']
> for p in paths:
> sys.path = [os.path.dirname(p)]
> f = os.path.join(sys.path[0], '__init__.py')
> file(, 'w')
> import os.path.splitext(os.path.basename(p))[0]
> os.unlink(f)
> sys.path = pathbk
>
> Basically, I'm clearing the pythonpath for each iteration through the
> files I'm wanting to import, make the pythonpath be solely the folder
> containing the file, create a __init__.py inside it, and import the
> module. Feels hacky, yeah. I hope there's some other way to acomplish
> this.
>
> I've been reading up about setuptools. It's something my head just
> doesn't want to absorb! I'll keep trying it though.
>
> Any tips on this?
>
>
> ~ Chris
>
> El dom, 23-03-2008 a las 22:27 +0100, Christian Vest Hansen escribió:
>
> > There's the pkg_resources module and .eggs. That might be what you're
> > looking for.
> >
> > Try typing this in a python repl:
> >
> > >>> import pkg_resources
> > >>> help(pkg_resources)
> >
> >
> >
> > On 3/23/08, Chris Hoeppner <[EMAIL PROTECTED]> wrote:
> > >
> > > Hey there!
> > >
> > > This has really nothing to do at all with django, beside the fact that
> > > many of it's developers should be able to answer from the top of their
> > > head. I couldn't think right now of any nice python community, and
> > > signing up somewhere else just to fire a single question and probably
> > > never ever return didn't seem like something I'd do on a sunday
> > > evening.
> > >
> > > For an app I'm writing, I'd love to know if it is at all possible to
> > > do the following workflow:
> > >
> > > * Given a list of paths, à la PYTHONPATH, go through those paths and
> > > it's subpaths and "discover" (aka, import) all python modules found
> > > * Loop through all of those modules searching for classes that are
> > > subclasses of a certain class, and add their objects to a list, or:
> > > * make the modules "execute" (I can't recall if this really happens
> > > upon import anyways, sorry), so that I could just use a decorator to
> > > do the add-to-a-list thing
> > >
> > > I'm a tad lost on this subject, and I'd really appreciate any pointers
> > > you could give me.
> > >
> > > ~ Chris
> > > >
> > >
> >
> >
>
>
> >
>
--
Venlig hilsen / Kind regards,
Christian Vest Hansen.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---