Setting PYTHONPATH from code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting PYTHONPATH from code

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).


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Oleksii Bidiuk
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet



--
oleksii

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Sharon Rozenblum

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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Lukáš Duběda
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] [hidden email]
[W] http://www.duber.cz

[A] R.A.Dvorského 601, Praha 10
[A] 10900, Czech Republic, Europe

On 23.2.2011 14:37, Sharon Rozenblum wrote:

> 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:[hidden email]]
> *Sent:* Thursday, February 10, 2011 2:21 PM
> *To:* Sharon Rozenblum
> *Cc:* [hidden email]
> *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 <[hidden email]
> <mailto:[hidden email]>>
>
> 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 - [hidden email]
> <mailto:[hidden email]>
> http://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
>
> --
> oleksii
>
>
>
> _________________________________________________
> Python.NET mailing list - [hidden email]
> http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Sharon Rozenblum
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:[hidden email]]
Sent: Wednesday, February 23, 2011 3:44 PM
To: Sharon Rozenblum
Cc: Oleksii Bidiuk; [hidden email]
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] [hidden email]
[W] http://www.duber.cz

[A] R.A.Dvorského 601, Praha 10
[A] 10900, Czech Republic, Europe

On 23.2.2011 14:37, Sharon Rozenblum wrote:

> 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:[hidden email]]
> *Sent:* Thursday, February 10, 2011 2:21 PM
> *To:* Sharon Rozenblum
> *Cc:* [hidden email]
> *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 <[hidden email]
> <mailto:[hidden email]>>
>
> 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 - [hidden email]
> <mailto:[hidden email]>
> http://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
>
> --
> oleksii
>
>
>
> _________________________________________________
> Python.NET mailing list - [hidden email]
> 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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Tribble, Brett
In reply to this post by Sharon Rozenblum

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=[hidden email] [mailto:pythondotnet-bounces+btribble=[hidden email]] On Behalf Of Sharon Rozenblum
Sent: Wednesday, February 23, 2011 5:37 AM
To: Oleksii Bidiuk
Cc: [hidden email]
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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Sharon Rozenblum

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:[hidden email]]
Sent: Wednesday, February 23, 2011 9:26 PM
To: Sharon Rozenblum; Oleksii Bidiuk
Cc: [hidden email]
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=[hidden email] [mailto:pythondotnet-bounces+btribble=[hidden email]] On Behalf Of Sharon Rozenblum
Sent: Wednesday, February 23, 2011 5:37 AM
To: Oleksii Bidiuk
Cc: [hidden email]
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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Benjamin Aranguren-2
In reply to this post by Sharon Rozenblum
On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <[hidden email]> 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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Sharon Rozenblum

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: [hidden email] [mailto:[hidden email]] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: [hidden email]
Subject: Re: [Python.NET] Setting PYTHONPATH from code

 

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <[hidden email]> 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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet

 


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Setting PYTHONPATH from code

Sharon Rozenblum
In reply to this post by Benjamin Aranguren-2

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: [hidden email]
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: [hidden email] [mailto:[hidden email]] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: [hidden email]
Subject: Re: [Python.NET] Setting PYTHONPATH from code

 

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <[hidden email]> 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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet

 


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

How to show the Console window

Sharon Rozenblum
In reply to this post by Benjamin Aranguren-2

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: '[hidden email]'; 'Benjamin Aranguren'
Cc: '[hidden email]'
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: [hidden email]
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: [hidden email] [mailto:[hidden email]] On Behalf Of Benjamin Aranguren
Sent: Sunday, February 27, 2011 6:08 PM
To: Sharon Rozenblum
Cc: [hidden email]
Subject: Re: [Python.NET] Setting PYTHONPATH from code

 

On Wed, Feb 23, 2011 at 5:37 AM, Sharon Rozenblum <[hidden email]> 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:[hidden email]]
Sent: Thursday, February 10, 2011 2:21 PM
To: Sharon Rozenblum
Cc: [hidden email]
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 <[hidden email]>

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet




--
oleksii


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet

 


_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: How to show the Console window

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!

2011/3/1 Sharon Rozenblum <[hidden email]>

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

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet