Help with embedding fully qualified script name

J

Jim Wallace

I'm trying to call a method in a script file given a fully qualified file
name, e.g. c:\myscript\test.py. I find that PyImport_Import("test") works
fine, with c:\myscript is set as the first item in the PYTHONPATH.

I tried PyRun_Simple and that does run my specific file, but then when I try
to use the calls below that I used on PyImport_Import, I get a null function
returned. I've tried the other methods that use a FILE and they do the same
thing.

pDict = PyModule_GetDict ( pModule );
pFunc = PyDict_GetItemString ( pDict, "MyFunction" );

I also tried doing things like creating a new module, etc. since I thought
that may work, but suspect that I was missing some steps.

The basic flow I want to do is this, with only the first step being my
problem.

1) Load the script file ??
2) Get the function "MyFunction" from the script (PyModule_GetDict,
PyDict_GetItemString)
3) Call that function PyObject_CallObject

I appreciate any help since I've spent quite a bit of time searching and
trying things.

Jim W.
 
K

Kamilche

To avoid pathname headaches, I've taken to including the following 3
lines at the top of every script that will be double-clicked:

import os, sys
pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname)
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top