Discussion:
[Python.NET] Setting PYTHONPATH from code
Sharon Rozenblum
2011-02-10 11:48:08 UTC
Permalink
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).
Oleksii Bidiuk
2011-02-10 12:20:54 UTC
Permalink
Hi Sharon,

What do you want to achieve with this? If you want to provide path for
loading your own modules you can change the sys.path value by e.g. running a
'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and
then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> +
"')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?
Post by Sharon Rozenblum
Hi!
Can someone tell me how to set PYTHONPATH variable in code when working
with Python.Runtime.dll without changing the value in the system variables
before.
Thanks
SHARON
------------------------------
PLEASE NOTE: The information contained in this electronic mail message is
intended only for the use of the designated recipient(s) named above. If the
reader of this message is not the intended recipient, you are hereby
notified that you have received this message in error and that any review,
dissemination, distribution, or copying of this message is strictly
prohibited. If you have received this communication in error, please notify
the sender by telephone or e-mail (as shown above) immediately and destroy
any and all copies of this message in your possession (whether hard copies
or electronically stored copies).
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
Sharon Rozenblum
2011-02-23 13:37:04 UTC
Permalink
Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{


}



I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the “modulePath” and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:***@gmail.com]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <***@sandisk.com<mailto:***@sandisk.com>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - ***@python.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
Lukáš Duběda
2011-02-23 13:44:29 UTC
Permalink
Hi Sharon,

I don't use Python.NET, but I use IronPython and this is a snippet
I use to add PYTHONPATH to the sys.path object:

I use system variables to do so (for various reasons) at the studio.

import sys
from System import Environment as sysEnv

PythonPath = sysEnv.GetEnvironmentVariable('PYTHONPATH')
sys.path.extend(PythonPath.split(';'))


Hope this helps.

Lukáš Duběda
Director
[T] +420 602 444 164

duber studio(tm)
[M] ***@duber.cz
[W] http://www.duber.cz

[A] R.A.Dvorského 601, Praha 10
[A] 10900, Czech Republic, Europe
Hi!
string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);
IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{

}

I got Null value from the ImportModule.
When setting the PYTHONPATH manually in the system variables with the
“modulePath” and opening the VS is works.
Any idea?
Thanks,
SAHRON
*Sent:* Thursday, February 10, 2011 2:21 PM
*To:* Sharon Rozenblum
*Subject:* Re: [Python.NET] Setting PYTHONPATH from code
Hi Sharon,
What do you want to achieve with this? If you want to provide path for
loading your own modules you can change the sys.path value by e.g.
running a 'initialization' script with
import sys
sys.path.insert(0, 'your/own/path')
You can build up the script above in C# by inserting the path you need
and then performing e.g. RunSimpleString(<script>) like
string script = "import sys\nsys.path.insert(0, '" +
<variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);
Does this help?
Hi!
Can someone tell me how to set PYTHONPATH variable in code when working
with Python.Runtime.dll without changing the value in the system
variables before.
Thanks
SHARON
------------------------------------------------------------------------
PLEASE NOTE: The information contained in this electronic mail message
is intended only for the use of the designated recipient(s) named above.
If the reader of this message is not the intended recipient, you are
hereby notified that you have received this message in error and that
any review, dissemination, distribution, or copying of this message is
strictly prohibited. If you have received this communication in error,
please notify the sender by telephone or e-mail (as shown above)
immediately and destroy any and all copies of this message in your
possession (whether hard copies or electronically stored copies).
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - ***@python.org
http://mail.python.org/ma
Sharon Rozenblum
2011-02-23 14:44:44 UTC
Permalink
Hi!

I need to run some python scripts at the same time where each one of them is in different location & their location is not in the machine environment variable.

So, I need to declare for each thread its PYTHONPATH internally...

Any idea?



-----Original Message-----
From: Lukáš Duběda [mailto:***@duber.cz]
Sent: Wednesday, February 23, 2011 3:44 PM
To: Sharon Rozenblum
Cc: Oleksii Bidiuk; ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

I don't use Python.NET, but I use IronPython and this is a snippet
I use to add PYTHONPATH to the sys.path object:

I use system variables to do so (for various reasons) at the studio.

import sys
from System import Environment as sysEnv

PythonPath = sysEnv.GetEnvironmentVariable('PYTHONPATH')
sys.path.extend(PythonPath.split(';'))


Hope this helps.

Lukáš Duběda
Director
[T] +420 602 444 164

duber studio(tm)
[M] ***@duber.cz
[W] http://www.duber.cz

[A] R.A.Dvorského 601, Praha 10
[A] 10900, Czech Republic, Europe
Hi!
string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);
IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{

}

I got Null value from the ImportModule.
When setting the PYTHONPATH manually in the system variables with the
“modulePath” and opening the VS is works.
Any idea?
Thanks,
SAHRON
*Sent:* Thursday, February 10, 2011 2:21 PM
*To:* Sharon Rozenblum
*Subject:* Re: [Python.NET] Setting PYTHONPATH from code
Hi Sharon,
What do you want to achieve with this? If you want to provide path for
loading your own modules you can change the sys.path value by e.g.
running a 'initialization' script with
import sys
sys.path.insert(0, 'your/own/path')
You can build up the script above in C# by inserting the path you need
and then performing e.g. RunSimpleString(<script>) like
string script = "import sys\nsys.path.insert(0, '" +
<variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);
Does this help?
Hi!
Can someone tell me how to set PYTHONPATH variable in code when working
with Python.Runtime.dll without changing the value in the system
variables before.
Thanks
SHARON
------------------------------------------------------------------------
PLEASE NOTE: The information contained in this electronic mail message
is intended only for the use of the designated recipient(s) named above.
If the reader of this message is not the intended recipient, you are
hereby notified that you have received this message in error and that
any review, dissemination, distribution, or copying of this message is
strictly prohibited. If you have received this communication in error,
please notify the sender by telephone or e-mail (as shown above)
immediately and destroy any and all copies of this message in your
possession (whether hard copies or electronically stored copies).
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - ***@python.org
http://mail.pyth
Tribble, Brett
2011-02-23 19:25:59 UTC
Permalink
This is the code I use all the time to append a dir to the system path and then load assemblies from that dir. (note, they’re not in the GAC {woot!})

if os.path.isfile(myFile):
if not myDir in sys.path:
sys.path.insert(0, myDir)
clr.AddReference("myAssembly")

myFile is the name of the assembly dll ala: “C:/testdir/myAssembly.dll”
myDir would be: “C:/testdir”

I always make sure that the namespace in the compiled assembly is the same as the filename. This seems to make things run more smoothly.

From: pythondotnet-bounces+btribble=***@python.org [mailto:pythondotnet-bounces+btribble=***@python.org] On Behalf Of Sharon Rozenblum
Sent: Wednesday, February 23, 2011 5:37 AM
To: Oleksii Bidiuk
Cc: ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{


}



I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the “modulePath” and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:***@gmail.com]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <***@sandisk.com<mailto:***@sandisk.com>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - ***@python.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
Sharon Rozenblum
2011-02-27 10:59:19 UTC
Permalink
Hi!

Did not understand the reply!

Do I need to change the PYTHONPATH from code?

Do I need to change the working directory in the C# program?

Why running the python script is not working?


Thanks,


SHARON

From: Tribble, Brett [mailto:***@ea.com]
Sent: Wednesday, February 23, 2011 9:26 PM
To: Sharon Rozenblum; Oleksii Bidiuk
Cc: ***@python.org
Subject: RE: [Python.NET] Setting PYTHONPATH from code

This is the code I use all the time to append a dir to the system path and then load assemblies from that dir. (note, they’re not in the GAC {woot!})

if os.path.isfile(myFile):
if not myDir in sys.path:
sys.path.insert(0, myDir)
clr.AddReference("myAssembly")

myFile is the name of the assembly dll ala: “C:/testdir/myAssembly.dll”
myDir would be: “C:/testdir”

I always make sure that the namespace in the compiled assembly is the same as the filename. This seems to make things run more smoothly.

From: pythondotnet-bounces+btribble=***@python.org [mailto:pythondotnet-bounces+btribble=***@python.org] On Behalf Of Sharon Rozenblum
Sent: Wednesday, February 23, 2011 5:37 AM
To: Oleksii Bidiuk
Cc: ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{


}



I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the “modulePath” and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:***@gmail.com]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: ***@python.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <***@sandisk.com<mailto:***@sandisk.com>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - ***@python.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
Benjamin Aranguren
2011-02-27 16:08:26 UTC
Permalink
On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <
Post by Sharon Rozenblum
Hi!
string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
Have you tried using a semi colon instead of new line?
string script = "import sys; sys.path.insert(0, '"
+ modulePath + "')";
Post by Sharon Rozenblum
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);
IntPtr gs = PythonEngine.AcquireLock();
pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{
…
}
…
I got Null value from the ImportModule.
When setting the PYTHONPATH manually in the system variables with the
“modulePath” and opening the VS is works.
Any idea?
Thanks,
SAHRON
*Sent:* Thursday, February 10, 2011 2:21 PM
*To:* Sharon Rozenblum
*Subject:* Re: [Python.NET] Setting PYTHONPATH from code
Hi Sharon,
What do you want to achieve with this? If you want to provide path for
loading your own modules you can change the sys.path value by e.g. running a
'initialization' script with
import sys
sys.path.insert(0, 'your/own/path')
You can build up the script above in C# by inserting the path you need and
then performing e.g. RunSimpleString(<script>) like
string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);
Does this help?
Hi!
Can someone tell me how to set PYTHONPATH variable in code when working
with Python.Runtime.dll without changing the value in the system variables
before.
Thanks
SHARON
------------------------------
PLEASE NOTE: The information contained in this electronic mail message is
intended only for the use of the designated recipient(s) named above. If the
reader of this message is not the intended recipient, you are hereby
notified that you have received this message in error and that any review,
dissemination, distribution, or copying of this message is strictly
prohibited. If you have received this communication in error, please notify
the sender by telephone or e-mail (as shown above) immediately and destroy
any and all copies of this message in your possession (whether hard copies
or electronically stored copies).
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
--
oleksii
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Sharon Rozenblum
2011-02-28 13:48:39 UTC
Permalink
Hi!

Tried after your mail.

Does not change...

Regarding the PYTHONPATH:


I change it with code (System.Environment.SetEnvironmentVariable("PYTHONPATH",PathValue)) and it does not help me at the first run of the application. (I am getting Null value from the ImportModule)
But, after closing the application & opening it again it works (event if changing the PYTHONPATH in the code) with the PYTHONPATH that was set in the run before...

Any Idea?

Thanks,

SHARON

From: baranguren-***@public.gmane.org [mailto:baranguren-***@public.gmane.org] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <***@sandisk.com<mailto:Sharon.Rozenblum-***@public.gmane.org>> wrote:
Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
Have you tried using a semi colon instead of new line?
string script = "import sys; sys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();




PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();




pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{
...
}
...

I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the "modulePath" and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:oleksii.bidiuk-***@public.gmane.org<mailto:***@gmail.com>]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org>
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <Sharon.Rozenblum-***@public.gmane.org<mailto:Sharon.Rozenblum-***@public.gmane.org>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet



--
oleksii

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
Sharon Rozenblum
2011-02-28 17:18:53 UTC
Permalink
Hi!

Checked again and found that I had in the end of the module path string the char:"\".

After deleting it - it works for me as oleksii.bidiuk said:

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);


( with the "\n" delimiter!)


Thanks to all!


SHARON

From: Sharon Rozenblum
Sent: Monday, February 28, 2011 3:49 PM
To: 'Benjamin Aranguren'
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: RE: [Python.NET] Setting PYTHONPATH from code

Hi!

Tried after your mail.

Does not change...

Regarding the PYTHONPATH:


I change it with code (System.Environment.SetEnvironmentVariable("PYTHONPATH",PathValue)) and it does not help me at the first run of the application. (I am getting Null value from the ImportModule)
But, after closing the application & opening it again it works (event if changing the PYTHONPATH in the code) with the PYTHONPATH that was set in the run before...

Any Idea?

Thanks,

SHARON

From: baranguren-***@public.gmane.org [mailto:baranguren-***@public.gmane.org] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <***@sandisk.com<mailto:Sharon.Rozenblum-***@public.gmane.org>> wrote:
Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
Have you tried using a semi colon instead of new line?
string script = "import sys; sys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();



PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();



pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{
...
}
...

I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the "modulePath" and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:oleksii.bidiuk-***@public.gmane.org<mailto:***@gmail.com>]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org>
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <Sharon.Rozenblum-***@public.gmane.org<mailto:Sharon.Rozenblum-***@public.gmane.org>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet



--
oleksii

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
Sharon Rozenblum
2011-03-01 10:06:15 UTC
Permalink
Hi!

Is there a way to show the console window when running Python from C# (in order to see the Python outputs, when the c# project is not console application)

Thanks,

SHARON



From: Sharon Rozenblum
Sent: Monday, February 28, 2011 7:19 PM
To: 'oleksii.bidiuk-***@public.gmane.org'; 'Benjamin Aranguren'
Cc: 'pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org'
Subject: RE: [Python.NET] Setting PYTHONPATH from code

Hi!

Checked again and found that I had in the end of the module path string the char:"\".

After deleting it - it works for me as oleksii.bidiuk said:

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);


( with the "\n" delimiter!)


Thanks to all!


SHARON

From: Sharon Rozenblum
Sent: Monday, February 28, 2011 3:49 PM
To: 'Benjamin Aranguren'
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: RE: [Python.NET] Setting PYTHONPATH from code

Hi!

Tried after your mail.

Does not change...

Regarding the PYTHONPATH:


I change it with code (System.Environment.SetEnvironmentVariable("PYTHONPATH",PathValue)) and it does not help me at the first run of the application. (I am getting Null value from the ImportModule)
But, after closing the application & opening it again it works (event if changing the PYTHONPATH in the code) with the PYTHONPATH that was set in the run before...

Any Idea?

Thanks,

SHARON

From: baranguren-***@public.gmane.org [mailto:baranguren-***@public.gmane.org] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: Re: [Python.NET] Setting PYTHONPATH from code

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <***@sandisk.com<mailto:Sharon.Rozenblum-***@public.gmane.org>> wrote:
Hi!

Trying to do as you said:


string script = "import sys\nsys.path.insert(0, '" + modulePath + "')";
Have you tried using a semi colon instead of new line?
string script = "import sys; sys.path.insert(0, '" + modulePath + "')";

IntPtr pythonLock = PythonEngine.AcquireLock();



PythonEngine.RunSimpleString(script);
PythonEngine.ReleaseLock(pythonLock);

And then:

IntPtr gs = PythonEngine.AcquireLock();



pyportal = PythonEngine.ImportModule(moduleName);
if (pyportal == null)
{
...
}
...

I got Null value from the ImportModule.

When setting the PYTHONPATH manually in the system variables with the "modulePath" and opening the VS is works.

Any idea?

Thanks,

SAHRON


From: Oleksii Bidiuk [mailto:oleksii.bidiuk-***@public.gmane.org<mailto:***@gmail.com>]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org>
Subject: Re: [Python.NET] Setting PYTHONPATH from code

Hi Sharon,

What do you want to achieve with this? If you want to provide path for loading your own modules you can change the sys.path value by e.g. running a 'initialization' script with

import sys
sys.path.insert(0, 'your/own/path')

You can build up the script above in C# by inserting the path you need and then performing e.g. RunSimpleString(<script>) like

string script = "import sys\nsys.path.insert(0, '" + <variable_with_path> + "')");
IntPtr pythonLock = AcquireLock();
RunSimpleString(script);
ReleaseLock(pythonLock);

Does this help?

2011/2/10 Sharon Rozenblum <Sharon.Rozenblum-***@public.gmane.org<mailto:Sharon.Rozenblum-***@public.gmane.org>>
Hi!

Can someone tell me how to set PYTHONPATH variable in code when working with Python.Runtime.dll without changing the value in the system variables before.

Thanks

SHARON


________________________________
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet



--
oleksii

_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org<mailto:***@python.org>
http://mail.python.org/mailman/listinfo/pythondotnet
Oleksii Bidiuk
2011-03-01 21:33:26 UTC
Permalink
Hi Sharon,

I haven't found a way to run a console, but I had achieved a similar effect
by redirecting the stdout and stderr from within Python (as suggested some
time ago by Guido himself in a related embedded CPython post) to a .NET
object and then show the incoming strings in a TextBlock (I am using a WPF
app that hosts Python.NET). Below is a very short description of what I have
done. Please let me know if this is indeed what you were looking for (or
perhaps somebody else), I will prepare a more detailed post then (need to
recap details from code).

To achieve having 'live' console output I have used some 'setup script' and
'clean up' scripts executed before and after the user-defined script. In the
setup script I first backup the curent value of sys.std* objects and then
assign my own (.NET) object to sys.stdout and in the clean up part I restore
the original std* values (similar to the sample for inserting certain path
to the sys.path discussed before).

To make use of the technique described above you need to have a .NET object
that implements the same interface as python stream (meaning write,
writelines, etc methods, see dir(sys.stdout) for all the details). In my
experience write() and writelines() is enough. Having that you can pass an
instance of such object to your Python code. As soon as you have the
information coming through your .NET object methods you can do things like
raising events, adding text to GUI, dump to a file, etc. It does require a
bit of additional work, (which I honestly already expected to see already in
Python.NET) and to some extend similar to the concept of console redirection
per script context in IronPython, although in IronPython it is done in a
'native' .NET way.

I am not sure this is THE best solution and other suggestions are more than
welcome!
Post by Sharon Rozenblum
Hi!
Is there a way to show the console window when running Python from C# (in
order to see the Python outputs, when the c# project is not console
application)
Thanks,
SHARON
--
oleksii
Seungweon Park
2013-04-05 17:39:04 UTC
Permalink
Hi Oleksii & Sharon,

If you've done this before, would you share which I can get a console
window in embedded Python.Net and get the Python string output from C#?

Thank you,
Spark.
Post by Oleksii Bidiuk
Hi Sharon,
I haven't found a way to run a console, but I had achieved a similar
effect by redirecting the stdout and stderr from within Python (as
suggested some time ago by Guido himself in a related embedded CPython
post) to a .NET object and then show the incoming strings in a TextBlock (I
am using a WPF app that hosts Python.NET). Below is a very short
description of what I have done. Please let me know if this is indeed what
you were looking for (or perhaps somebody else), I will prepare a more
detailed post then (need to recap details from code).
To achieve having 'live' console output I have used some 'setup script'
and 'clean up' scripts executed before and after the user-defined script.
In the setup script I first backup the curent value of sys.std* objects and
then assign my own (.NET) object to sys.stdout and in the clean up part I
restore the original std* values (similar to the sample for inserting
certain path to the sys.path discussed before).
To make use of the technique described above you need to have a .NET
object that implements the same interface as python stream (meaning write,
writelines, etc methods, see dir(sys.stdout) for all the details). In my
experience write() and writelines() is enough. Having that you can pass an
instance of such object to your Python code. As soon as you have the
information coming through your .NET object methods you can do things like
raising events, adding text to GUI, dump to a file, etc. It does require a
bit of additional work, (which I honestly already expected to see already
in Python.NET) and to some extend similar to the concept of console
redirection per script context in IronPython, although in IronPython it is
done in a 'native' .NET way.
I am not sure this is THE best solution and other suggestions are more
than welcome!
Post by Sharon Rozenblum
Hi!
Is there a way to show the console window when running Python from C# (in
order to see the Python outputs, when the c# project is not console
application)
Thanks,
SHARON
--
oleksii
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...