How to run Python code from MAtlab

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dima81
    New Member
    • May 2010
    • 9

    How to run Python code from MAtlab

    I have 2 codes; one in Matlab and the other in Python.
    what i need to do is run the Python from Matlab.

    I know that i can use SYSTEM'....' command, but it is only openning the .PY in the python terminal and not running it itself.

    I need that when i call python script it will run automatically!
    Is there any suggestions?
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    What exactly are you using as the system command?

    It should be "python yourfile.py", right?

    Comment

    • dima81
      New Member
      • May 2010
      • 9

      #3
      Originally posted by Glenton
      What exactly are you using as the system command?

      It should be "python yourfile.py", right?
      hey!
      in MATLAB:

      system('abc.py' )

      it is openning the Python Terminal with the abc.py, but to in order to run it i have to run it myself.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Originally posted by dima81
        hey!
        in MATLAB:

        system('abc.py' )

        it is openning the Python Terminal with the abc.py, but to in order to run it i have to run it myself.
        Try

        system('python abc.py')

        Comment

        • dima81
          New Member
          • May 2010
          • 9

          #5
          Originally posted by Glenton
          Try

          system('python abc.py')
          i tried it already.
          it does not work...

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Okay. I think step 1 is to try to run your python program from command line (are you using windows?). Does this work? Is it possible that python isn't in your path? Or that there's some other problem.

            I'm afraid I don't have matlab, and anyway I run linux, so can't be of much help here, but this forum seems to suggest that running
            Code:
            !c:\python\...whateveryourpathis\python.exe(or whatever) abc.py
            should do it.

            Of course you could always migrate over to python completely... ;P

            Comment

            • dima81
              New Member
              • May 2010
              • 9

              #7
              Originally posted by Glenton
              Okay. I think step 1 is to try to run your python program from command line (are you using windows?). Does this work? Is it possible that python isn't in your path? Or that there's some other problem.

              I'm afraid I don't have matlab, and anyway I run linux, so can't be of much help here, but this forum seems to suggest that running
              Code:
              !c:\python\...whateveryourpathis\python.exe(or whatever) abc.py
              should do it.

              Of course you could always migrate over to python completely... ;P
              Hi Glenton!
              tnx for your answer!
              i am using Windows.
              the command:
              !c:\python\...w hateveryourpath is\python.exe(o r whatever) abc.py
              do exactly the same operation as command:
              system('abc.py' )

              it just opens the file abc.py in the Python terminal but not runs it! and this is my problem. i need it to run when i call it.

              anyway, thank you for your help!

              Comment

              • Glenton
                Recognized Expert Contributor
                • Nov 2008
                • 391

                #8
                What happens when you type python abc.py in your command line/dos prompt.

                Comment

                • dima81
                  New Member
                  • May 2010
                  • 9

                  #9
                  Originally posted by Glenton
                  What happens when you type python abc.py in your command line/dos prompt.
                  c:\users\Dima>p ython ab_test.py
                  'python is not recognized as an internal or external command,
                  operable program or batch file

                  Comment

                  • Glenton
                    Recognized Expert Contributor
                    • Nov 2008
                    • 391

                    #10
                    Well, if you fix that, you might be in with a chance.

                    I'm assuming python is actually installed!

                    You basically need to add python to the path:
                    1. Find the path where the python executable is. (probably c:\python\bin, or c:\python, or maybe c:\python26 or c:\python26\bin or something)
                    2. Add this path to the end of your path variable (separated from the others by a semi-colon). Your path variable can be adjusted by going to (something like) my computer - properties - advanced (or maybe system) - environment variables. I'm going from memory, and haven't used windows for a while. But I'm sure you'll manage.

                    Comment

                    • dima81
                      New Member
                      • May 2010
                      • 9

                      #11
                      Originally posted by Glenton
                      Well, if you fix that, you might be in with a chance.

                      I'm assuming python is actually installed!

                      You basically need to add python to the path:
                      1. Find the path where the python executable is. (probably c:\python\bin, or c:\python, or maybe c:\python26 or c:\python26\bin or something)
                      2. Add this path to the end of your path variable (separated from the others by a semi-colon). Your path variable can be adjusted by going to (something like) my computer - properties - advanced (or maybe system) - environment variables. I'm going from memory, and haven't used windows for a while. But I'm sure you'll manage.
                      thanks again!
                      i did as you said.
                      now when i am writing (in command line) "python" it is starting the Python. but when i enter "abc.py" i get an error:

                      traceback<inner most last>:
                      file "<stdin>", line 1,in ?
                      nameError: abc.py

                      When i'm writing "python abc.py" straight in the command line (without entering first to the python) i get an error:
                      python: can't open file 'abc.py'

                      Comment

                      • Glenton
                        Recognized Expert Contributor
                        • Nov 2008
                        • 391

                        #12
                        What directory is your command prompt in when you write the command? It should be the same directory as your abc.py. You can change directory with the cd command, and you can see the contents of your directory with the dir or ls commands.

                        Otherwise you could try to give the full path to abc.py, so in command line type
                        python C:/spam/eggs/spam/abc.py

                        Comment

                        Working...