Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. python and C#

python and C#

Scheduled Pinned Locked Moved C#
csharppythontoolsquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lune12

    I have a script in python. I would like to call it from a winform in C#, is it possible? and how? Thanks

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    If it works for you from a "command prompt", then Process.Start() can handle it. :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    L 1 Reply Last reply
    0
    • L Luc Pattyn

      If it works for you from a "command prompt", then Process.Start() can handle it. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      L Offline
      L Offline
      lune12
      wrote on last edited by
      #3

      Thanks. and if I want to call a single function from the script, is there a way?

      L 1 Reply Last reply
      0
      • L lune12

        Thanks. and if I want to call a single function from the script, is there a way?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        If you know how to do it from a "command prompt", then Process.Start() can handle it. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          If you know how to do it from a "command prompt", then Process.Start() can handle it. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          L Offline
          L Offline
          lune12
          wrote on last edited by
          #5

          no I don't know, can you explain please

          L C 2 Replies Last reply
          0
          • L lune12

            no I don't know, can you explain please

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            I can not. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • L lune12

              no I don't know, can you explain please

              C Offline
              C Offline
              candogu
              wrote on last edited by
              #7

              I think you can. Because from Csharp we can call another dll's functions by importing them with DllImport. However your python script should be a dll that registered to the system once ;)

              L 1 Reply Last reply
              0
              • C candogu

                I think you can. Because from Csharp we can call another dll's functions by importing them with DllImport. However your python script should be a dll that registered to the system once ;)

                L Offline
                L Offline
                lune12
                wrote on last edited by
                #8

                do you know how can I turn the python script into a dll. Or should I forward the question to the phyton forum?

                C 1 Reply Last reply
                0
                • L lune12

                  I have a script in python. I would like to call it from a winform in C#, is it possible? and how? Thanks

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  Yes it is. You need to install Iron Python[^. Then you gain access to your Python functions directly from .NET.

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  1 Reply Last reply
                  0
                  • L lune12

                    I have a script in python. I would like to call it from a winform in C#, is it possible? and how? Thanks

                    W Offline
                    W Offline
                    Wayne Gaylard
                    wrote on last edited by
                    #10

                    You can use IronPython[^] in a C# application. A simple implementation of loading a python file using IronPython looks something like this:-

                    Console.WriteLine("Loading discountprice.py");
                    ScriptRuntime py = Python.CreateRuntime();
                    dynamic random = py.UseFile(@"D:\Documents\Python Scripts\discountprice.py");
                    Console.WriteLine("discountprice.py loaded.");

                    that snippet loads a basic python file called discountprice.py in my python scripts folder. To implement this you need to download IronPython and reference Microsoft.Scripting.Hosting and also IronPython.Hosting. Hope this helps

                    When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                    L 1 Reply Last reply
                    0
                    • W Wayne Gaylard

                      You can use IronPython[^] in a C# application. A simple implementation of loading a python file using IronPython looks something like this:-

                      Console.WriteLine("Loading discountprice.py");
                      ScriptRuntime py = Python.CreateRuntime();
                      dynamic random = py.UseFile(@"D:\Documents\Python Scripts\discountprice.py");
                      Console.WriteLine("discountprice.py loaded.");

                      that snippet loads a basic python file called discountprice.py in my python scripts folder. To implement this you need to download IronPython and reference Microsoft.Scripting.Hosting and also IronPython.Hosting. Hope this helps

                      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                      L Offline
                      L Offline
                      lune12
                      wrote on last edited by
                      #11

                      Thanks it helps. (tried it with a simple script) my question now is: I have a python script that import module IntelHex. when executing the script itself it works properly. when calling it frm my C# application, I got an exception "No module named intelHex" Do you have a clue how can I solve this? Thanks,

                      1 Reply Last reply
                      0
                      • L lune12

                        do you know how can I turn the python script into a dll. Or should I forward the question to the phyton forum?

                        C Offline
                        C Offline
                        candogu
                        wrote on last edited by
                        #12

                        I don't know how to do that but, if you know what that pyhon script do, you can write it yourself in csharp as a dll file.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups