freelanceprogrammers.org Forum Index » Delphi

import error im the main scripy


View user's profile Post To page top
daydaysy Posted: Thu Jul 14, 2005 4:46 pm


Joined: 21 May 2005

Posts: 13
import error im the main scripy
I have a.py and b.py in the current directory.
And a.py has a line "import b",
When I run "pybthon.exe a.py" in the command line,
it works fine.
But when I run a.py in delphi by using PythonEngine.ExecuteStrings, I
got the following Error:

Traceback (most recent call last):
File "<string>", line 4, in ?
ImportError: No module named b

How did this happen?
And what should I do?

Thank you in advance.
Reply with quote
Send private message
View user's profile Post To page top
morgan_martinet Posted: Thu Jul 14, 2005 7:22 pm


Joined: 29 Apr 2005

Posts: 103
import error im the main scripy
> I have a.py and b.py in the current directory.
> And a.py has a line "import b",
> When I run "pybthon.exe a.py" in the command line,
> it works fine.
> But when I run a.py in delphi by using PythonEngine.ExecuteStrings, I
> got the following Error:
>
> Traceback (most recent call last):
> File "<string>", line 4, in ?
> ImportError: No module named b
>
> How did this happen?
> And what should I do?
Where did you put your a and b files?
If they are not located in the same folder as your .exe, Python won`t be
able to find them unless you add your custom folder to the Python Path.
You can do it either with a simple Python code:
import sys
sys.path.append("c:\myfolder")

Note that you can use the InitScript property of TPythonEngine to put your
script. You can either generate it and assign it to the property in the
OnBeforeLoad event, or you can write your script in a way that uses
sys.argv[0] to deduce the application path (same as Application.ExeName).

You can do the same code using VarPyth:
Uses VarPyth;
SysModule.path.append(`c:myfolder`)

Or you can use the event OnSysPathInit in TPythonEngine, to do the same
code:
procedure TForm1.PythonEngine1SysPathInit(Sender: TObject;
PathList: PPyObject);
Var
_folder : PPyObject;
Begin
_folder := PyString_FromString(PChar(Application.ExeName+`myFolder`));
PythonEngine1.PyList_Append(PathList, _folder);
PythonEngine1.Py_DecRef(_folder);
// or using VarPyth:
VarPythonCreate(PathList).append(Application.ExeName+`myFolder`);
end;

Hope that helps,

Morgan
Reply with quote
Send private message
View user's profile Post To page top
daydaysy Posted: Fri Jul 15, 2005 7:08 am


Joined: 21 May 2005

Posts: 13
import error im the main scripy
Thank you very much for your help.
P4D is wonderful~

--- In pythonfordelphi@yahoogroups.com, "Morgan Martinet" <yahoo@m...>
wrote:
> > I have a.py and b.py in the current directory.
> > And a.py has a line "import b",
> > When I run "pybthon.exe a.py" in the command line,
> > it works fine.
> > But when I run a.py in delphi by using PythonEngine.ExecuteStrings, I
> > got the following Error:
> >
> > Traceback (most recent call last):
> > File "<string>", line 4, in ?
> > ImportError: No module named b
> >
> > How did this happen?
> > And what should I do?
> Where did you put your a and b files?
> If they are not located in the same folder as your .exe, Python won`t be
> able to find them unless you add your custom folder to the Python Path.
> You can do it either with a simple Python code:
> import sys
> sys.path.append("c:\myfolder")
>
> Note that you can use the InitScript property of TPythonEngine to
put your
> script. You can either generate it and assign it to the property in the
> OnBeforeLoad event, or you can write your script in a way that uses
> sys.argv[0] to deduce the application path (same as
Application.ExeName).
>
> You can do the same code using VarPyth:
> Uses VarPyth;
> SysModule.path.append(`c:myfolder`)
>
> Or you can use the event OnSysPathInit in TPythonEngine, to do the same
> code:
> procedure TForm1.PythonEngine1SysPathInit(Sender: TObject;
> PathList: PPyObject);
> Var
> _folder : PPyObject;
> Begin
> _folder := PyString_FromString(PChar(Application.ExeName+`myFolder`));
> PythonEngine1.PyList_Append(PathList, _folder);
> PythonEngine1.Py_DecRef(_folder);
> // or using VarPyth:
> VarPythonCreate(PathList).append(Application.ExeName+`myFolder`);
> end;
>
> Hope that helps,
>
> Morgan
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help