importing with .m instead of .py

Discussion in 'Python' started by Wanderer, Sep 25, 2009.

  1. Wanderer

    Wanderer Guest

    I would like to import Matlab/Octave files of the .m sort into Python
    that look like this.

    # comment
    y=[1,2,3,4,5\
    ,6,7,8,9];
    # comment

    The only problem is I have to change the extensions from .m to .py. Is
    there a way to get python to import files that don't end in .py?

    Thank you
    Wanderer, Sep 25, 2009
    #1
    1. Advertising

  2. Wanderer

    Cesar Koers Guest

    Wanderer wrote:
    > I would like to import Matlab/Octave files of the .m sort into Python
    > that look like this.
    >
    > # comment
    > y=[1,2,3,4,5\
    > ,6,7,8,9];
    > # comment
    >
    > The only problem is I have to change the extensions from .m to .py. Is
    > there a way to get python to import files that don't end in .py?
    >
    > Thank you

    Hi,

    (untested:) read the .m file as a string, then evaluate the string (see
    eval()) in appropriate context?


    success!

    bye
    Cesar Koers, Sep 25, 2009
    #2
    1. Advertising

  3. Wanderer
    Hi
    Refer to http://docs.python.org/tutorial/modules.html#the-module-search-path.

    Particularly-
    "
    When a module named spam is imported, the interpreter searches for a
    file named spam.py in the current directory, and then in the list of
    directories specified by the environment variable PYTHONPATH. This has
    the same syntax as the shell variable PATH, that is, a list of
    directory names. When PYTHONPATH is not set, or when the file is not
    found there, the search continues in an installation-dependent default
    path; on Unix, this is usually .:/usr/local/lib/python.
    "

    Having said that, please see below.

    > I would like to import Matlab/Octave files of the .m sort into Python
    > that look like this.
    >
    > # comment
    > y=[1,2,3,4,5\
    > ,6,7,8,9];
    > # comment
    >
    > The only problem is I have to change the extensions from .m to .py. Is
    > there a way to get python to import files that don't end in .py?


    You can try this for single file. Pretty trivial-
    $ cat foo.m
    y=[1,2,3]
    $ export PYTHONSTARTUP=foo.m
    $ python
    Python 2.6.1 (r261:826, Sep 17 2009, 01:16:52)
    [GCC 4.3.2] on linux2
    [Unladen Swallow 2009Q3]
    Type "help", "copyright", "credits" or "license" for more information.
    >>> dir()

    ['__builtins__', '__doc__', '__name__', '__package__', 'y']
    >>> y

    [1, 2, 3]

    Another way:
    >>> f_ = open('foo.m','r');
    >>> j = f_.xreadlines()
    >>> for i in j.xreadlines():

    .... print i
    ....
    y=[1,2,3]

    Another way:
    >>> import os;
    >>> matlab_files=[];
    >>> for root, dirs, files in os.walk('.'):

    .... for i in files:
    .... if i.endswith(".m"):
    .... matlab_files.append(i);
    ....
    >>> matlab_files

    ['foo.m', 'bar.m']
    >>> for x in matlab_files:

    .... execfile(x);
    >>> dir()

    ['__builtins__', '__doc__', '__name__', 'dirs', 'files', 'i',
    'matlab_files', 'os', 'root', 'x', 'y', 'z']
    >>> x

    'b.m'
    >>> y

    [1, 2, 3]
    >>> z

    [3, 2, 1]

    $ cat foo.m
    y=[1,2,3]
    $ cat bar.m
    z=[3,2,1]

    These sort of task are pretty trivial to do. You should take some time
    to read through the documentation.. and oh don't be such a wanderer
    loosing sight of such good resource such as http://docs.python.org :)
    --
    Regards,
    Ishwor Gurung
    Ishwor Gurung, Sep 25, 2009
    #3
  4. Wanderer

    Wanderer Guest

    On Sep 25, 12:15 pm, Ishwor Gurung <> wrote:
    > Wanderer
    > Hi
    > Refer tohttp://docs.python.org/tutorial/modules.html#the-module-search-path.
    >
    > Particularly-
    > "
    > When a module named spam is imported, the interpreter searches for a
    > file named spam.py in the current directory, and then in the list of
    > directories specified by the environment variable PYTHONPATH. This has
    > the same syntax as the shell variable PATH, that is, a list of
    > directory names. When PYTHONPATH is not set, or when the file is not
    > found there, the search continues in an installation-dependent default
    > path; on Unix, this is usually .:/usr/local/lib/python.
    > "
    >
    > Having said that, please see below.
    >
    > > I would like to import Matlab/Octave files of the .m sort into Python
    > > that look like this.

    >
    > > # comment
    > > y=[1,2,3,4,5\
    > > ,6,7,8,9];
    > > # comment

    >
    > > The only problem is I have to change the extensions from .m to .py. Is
    > > there a way to get python to import files that don't end in .py?

    >
    > You can try this for single file. Pretty trivial-
    > $ cat foo.m
    > y=[1,2,3]
    > $ export PYTHONSTARTUP=foo.m
    > $ python
    > Python 2.6.1 (r261:826, Sep 17 2009, 01:16:52)
    > [GCC 4.3.2] on linux2
    > [Unladen Swallow 2009Q3]
    > Type "help", "copyright", "credits" or "license" for more information.>>> dir()
    >
    > ['__builtins__', '__doc__', '__name__', '__package__', 'y']>>> y
    >
    > [1, 2, 3]
    >
    > Another way:>>> f_ = open('foo.m','r');
    > >>> j = f_.xreadlines()
    > >>> for i in j.xreadlines():

    >
    > ...     print i
    > ...
    > y=[1,2,3]
    >
    > Another way:>>> import os;
    > >>> matlab_files=[];
    > >>> for root, dirs, files in os.walk('.'):

    >
    > ...     for i in files:
    > ...             if i.endswith(".m"):
    > ...                     matlab_files.append(i);
    > ...>>> matlab_files
    > ['foo.m', 'bar.m']
    > >>> for x in matlab_files:

    >
    > ...     execfile(x);>>> dir()
    >
    > ['__builtins__', '__doc__', '__name__', 'dirs', 'files', 'i',
    > 'matlab_files', 'os', 'root', 'x', 'y', 'z']>>> x
    > 'b.m'
    > >>> y

    > [1, 2, 3]
    > >>> z

    >
    > [3, 2, 1]
    >
    > $ cat foo.m
    > y=[1,2,3]
    > $ cat bar.m
    > z=[3,2,1]
    >
    > These sort of task are pretty trivial to do. You should take some time
    > to read through the documentation.. and oh don't be such a wanderer
    > loosing sight of such good resource such ashttp://docs.python.org:)
    > --
    > Regards,
    > Ishwor Gurung


    execfile(x) does what I'm looking for.

    Thanks
    Wanderer, Sep 25, 2009
    #4
  5. On Sep 25, 1:16 pm, Wanderer <> wrote:
    > execfile(x) does what I'm looking for.
    >


    Perhaps you are looking for Python import hook: http://www.python.org/dev/peps/pep-0302/

    In you import hook you can do everything (and locate/import file with
    any extension :)
    Andrew Svetlov, Sep 25, 2009
    #5
  6. Wanderer

    Wanderer Guest

    On Sep 25, 2:05 pm, Andrew Svetlov <> wrote:
    > On Sep 25, 1:16 pm, Wanderer <> wrote:
    >
    > > execfile(x) does what I'm looking for.

    >
    > Perhaps you are looking for Python import hook:http://www.python.org/dev/peps/pep-0302/
    >
    > In you import hook you can do everything (and locate/import file with
    > any extension :)


    I'm just porting a bunch of octave functions into pylab that use data
    I collected in files like I show above. The execfile() method lets me
    verify I get the same results in Octave and Python. It is in the NumPy
    for Matlab users page at Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html,
    but they make it look like it needs a .py extension. Like Ishwor said
    'Trivial', now that I know how :)
    Wanderer, Sep 25, 2009
    #6
  7. Wanderer

    wisccal Guest

    Wanderer wrote:
    > Is there a way to get python to import files that don't end in .py?


    How about using imp.load_source?

    y@x:/tmp$ cat out.m
    print("this is my module")
    # comment
    y=[1,2,3,4,5,6,7,8,9];
    # comment
    y@x:/tmp$ cat load_my_module.py
    import imp
    MyModule=imp.load_source('MyModule','out.m')
    print("y is {0}".format(MyModule.y))
    y@x:/tmp$ p31 load_my_module.py
    this is my module
    y is [1, 2, 3, 4, 5, 6, 7, 8, 9]

    Regards
    wisccal
    http://skre.ch
    wisccal, Sep 26, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Awah Teh

    Hyper-efficient Text Importing

    Awah Teh, Sep 2, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,159
    William Tasso
    Sep 3, 2003
  2. Marcus Schaemann
    Replies:
    1
    Views:
    1,071
    Eyck Jentzsch
    Sep 17, 2003
  3. Matt Breedlove
    Replies:
    1
    Views:
    424
    Eduard W. Lohmann
    Nov 24, 2003
  4. Lakshmi

    importing Aspx page into ASP page

    Lakshmi, Jul 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    Kevin Spencer
    Jul 2, 2003
  5. plb
    Replies:
    2
    Views:
    348
Loading...

Share This Page