472,333 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

Loading module via full path

Hi all.

Is there a way to load a module given a full path to the module
without extending sys.path first?

Andre'

Aug 21 '06 #1
3 1773
Andre Poenitz wrote:
Hi all.

Is there a way to load a module given a full path to the module
without extending sys.path first?

Andre'

The standard module named "imp" can help you with this.

Gary Herron
Aug 21 '06 #2
Andre Poenitz <an***@svn2.wasy.dewrote:
Hi all.

Is there a way to load a module given a full path to the module
without extending sys.path first?
Ok. imp.find_module() and imp.load_module() seem to do what I need.

Andre'
Aug 22 '06 #3
Gary Herron <gh*****@islandtraining.comwrote:
Andre Poenitz wrote:
>Hi all.

Is there a way to load a module given a full path to the module
without extending sys.path first?

The standard module named "imp" can help you with this.
Thank you. I got stuck on http://docs.python.org/api/importing.html
(as I need it to be called from C(++)) and found no way to specify a path there
[and there aren't too many cross references in the python docs *sigh*]

I have now something similar to

class CPyObject
{
public:
explicit CPyObject(PyObject * pObject) : m_pObject(pObject) { /*Check();*/ }
~CPyObject() { Py_XDECREF(m_pObject); }
operator PyObject *() { return m_pObject; }
private:
CPyObject(const CPyObject &); // intentionally not implemented
void operator=(const CPyObject &); // intentionally not implemented
PyObject * m_pObject;
};

static PyObject * LoadModule(const char * mod, const char * path)
{
CPyObject pyImpModule ( PyImport_Import(PyString_FromString("imp")) );

CPyObject pyImpFindModuleFunc ( PyObject_GetAttrString(pyImpModule, "find_module") );
CPyObject pyImpFindModuleArgs ( Py_BuildValue("s[s]", mod, path) );
CPyObject pyImpFindModuleRes ( PyObject_CallObject(pyImpFindModuleFunc, pyImpFindModuleArgs) );

CPyObject pyImpLoadModuleFunc ( PyObject_GetAttrString(pyImpModule, "load_module") );
CPyObject pyImpLoadModuleArgs ( Py_BuildValue("sOOO",
mod,
PyTuple_GetItem(pyImpFindModuleRes, 0),
PyTuple_GetItem(pyImpFindModuleRes, 1),
PyTuple_GetItem(pyImpFindModuleRes, 2)
));
return PyObject_CallObject(pyImpLoadModuleFunc, pyImpLoadModuleArgs);
}

which seems to do what I want even if it looks a bit too verbose for my taste and I
don't know whether I go the reference counting right.

Thanks for the hint,
Andre'
Aug 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: saif | last post by:
Hi, My problem is that php is not loading ANY module. Its saying that can not find path. Although I have used different style path names like:...
5
by: Pete Marsh | last post by:
Wondering if anyone can recomend some sample code for dynamically loading the GD module. I have tried setting the extension dir in php.ini, and...
15
by: pdhb_1 | last post by:
I'm trying to run Apache2.2.6 with php. However, I keep getting a long list of error messages in the log file indicating that includes are failing....
1
by: PALLAVI SHARMA | last post by:
Sir, i have configure the ms access file on webserver. MS. Access file is showing in .../db/mohan.mdb on web server. could you tell me what path...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.