Setting PYTHONPATH from Makefile

Discussion in 'Python' started by pinkfloydhomer@gmail.com, Dec 2, 2005.

  1. Guest

    I have a Makefile target that uses a python script, like:

    %.abc: %.def
    python myscript.py

    The problem is that myscript.py and some modules that myscript.py
    imports are not in the current directory, but in another place in the
    filesystem, say, /path/to/stuff. If this was a tcsh script, I would
    just do:

    setenv PYTHONPATH /path/to/stuff
    python myscript.py

    but this cannot be done from a Makefile. So what do I do? Is there
    another way to set the PYTHONPATH? Like giving an option to "python"
    itself? Or?

    /David
     
    , Dec 2, 2005
    #1
    1. Advertising

  2. []
    > I have a Makefile target that uses a python script, like:


    > %.abc: %.def
    > python myscript.py


    > If this was a tcsh script, I would just do:


    > setenv PYTHONPATH /path/to/stuff
    > python myscript.py


    > but this cannot be done from a Makefile.


    Use:

    %.abc: %.def
    PYTHONPATH=/path/to/stuff python myscript.py

    In fact, within Make or outside Make, for any shell command, you may
    write:

    VAR1=VALUE1 VAR2=VALUE2 ... COMMAND ARGUMENTS

    so temporarily setting VAR1, VAR2... in the environment for the duration
    of COMMAND only. This is a useful feature of the shell.

    --
    Fran├žois Pinard http://pinard.progiciels-bpi.ca
     
    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Dec 2, 2005
    #2
    1. Advertising

  3. Mike Meyer Guest

    "" <> writes:
    > I have a Makefile target that uses a python script, like:
    >
    > %.abc: %.def
    > python myscript.py
    >
    > The problem is that myscript.py and some modules that myscript.py
    > imports are not in the current directory, but in another place in the
    > filesystem, say, /path/to/stuff. If this was a tcsh script, I would
    > just do:
    >
    > setenv PYTHONPATH /path/to/stuff
    > python myscript.py


    And that still wouldn't work, because you said that myscript.py wasn't
    in the current directory either.

    > but this cannot be done from a Makefile. So what do I do? Is there
    > another way to set the PYTHONPATH? Like giving an option to "python"
    > itself? Or?


    No, you can't. You have to tweak the makefile. Try:

    %.abc: %.def
    (cd /path/to/myscripts/dir; python myscript.py)


    N.B. - this probably depends on both the system and the make you're
    using.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 3, 2005
    #3
  4. Inyeol Lee Guest

    On Fri, Dec 02, 2005 at 07:33:20PM -0500, Mike Meyer wrote:
    > "" <> writes:
    > > I have a Makefile target that uses a python script, like:
    > >
    > > %.abc: %.def
    > > python myscript.py
    > >
    > > The problem is that myscript.py and some modules that myscript.py
    > > imports are not in the current directory, but in another place in the
    > > filesystem, say, /path/to/stuff. If this was a tcsh script, I would
    > > just do:
    > >
    > > setenv PYTHONPATH /path/to/stuff
    > > python myscript.py

    >
    > And that still wouldn't work, because you said that myscript.py wasn't
    > in the current directory either.
    >
    > > but this cannot be done from a Makefile. So what do I do? Is there
    > > another way to set the PYTHONPATH? Like giving an option to "python"
    > > itself? Or?

    >
    > No, you can't. You have to tweak the makefile. Try:
    >
    > %.abc: %.def
    > (cd /path/to/myscripts/dir; python myscript.py)
    >
    >
    > N.B. - this probably depends on both the system and the make you're
    > using.
    >


    How about using python -m?
    Assuming Make uses Bourne shell,

    %.abc: %.def
    PYTHONPATH=/path/to/stuff:/path/to/another python -m myscript


    Don't forget to strip '.py' extension.

    --Inyeol Lee
     
    Inyeol Lee, Dec 3, 2005
    #4
  5. Mike Meyer Guest

    Inyeol Lee <> writes:
    > On Fri, Dec 02, 2005 at 07:33:20PM -0500, Mike Meyer wrote:
    >> > The problem is that myscript.py and some modules that myscript.py
    >> > imports are not in the current directory, but in another place in the
    >> > filesystem, say, /path/to/stuff. If this was a tcsh script, I would
    >> > just do:
    >> >
    >> > setenv PYTHONPATH /path/to/stuff
    >> > python myscript.py

    > How about using python -m?
    > Assuming Make uses Bourne shell,
    >
    > %.abc: %.def
    > PYTHONPATH=/path/to/stuff:/path/to/another python -m myscript


    That will break __name__ (at least with 2.4.2). Whether or not it
    matters will depend on the script.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 3, 2005
    #5
  6. Inyeol Lee Guest

    On Fri, Dec 02, 2005 at 08:10:41PM -0500, Mike Meyer wrote:
    > Inyeol Lee <> writes:
    > > On Fri, Dec 02, 2005 at 07:33:20PM -0500, Mike Meyer wrote:
    > >> > The problem is that myscript.py and some modules that myscript.py
    > >> > imports are not in the current directory, but in another place in the
    > >> > filesystem, say, /path/to/stuff. If this was a tcsh script, I would
    > >> > just do:
    > >> >
    > >> > setenv PYTHONPATH /path/to/stuff
    > >> > python myscript.py

    > > How about using python -m?
    > > Assuming Make uses Bourne shell,
    > >
    > > %.abc: %.def
    > > PYTHONPATH=/path/to/stuff:/path/to/another python -m myscript

    >
    > That will break __name__ (at least with 2.4.2). Whether or not it
    > matters will depend on the script.
    >


    $ python -V
    Python 2.4.2
    $ cat foo/bar.py
    print __name__
    $ (cd foo; python bar.py)
    __main__
    $ PYTHONPATH=$PWD/foo python -m bar
    __main__
    $


    Am I missing something? I don't see any issue regarding __name__.

    -- Inyeol Lee
     
    Inyeol Lee, Dec 3, 2005
    #6
    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. Harald Kirsch

    jython and PYTHONPATH

    Harald Kirsch, Jun 28, 2003, in forum: Python
    Replies:
    1
    Views:
    1,300
    Ype Kingma
    Jun 30, 2003
  2. vincent delft

    CGIHTTPserver looze PYTHONPATH

    vincent delft, Jul 13, 2003, in forum: Python
    Replies:
    1
    Views:
    472
    vincent_delft
    Jul 13, 2003
  3. Terry Hancock

    Re: setting PYTHONPATH

    Terry Hancock, Mar 6, 2006, in forum: Python
    Replies:
    0
    Views:
    393
    Terry Hancock
    Mar 6, 2006
  4. per
    Replies:
    4
    Views:
    4,836
    Carl Banks
    Mar 1, 2009
  5. fl
    Replies:
    2
    Views:
    435
    88888 Dihedral
    Dec 24, 2011
Loading...

Share This Page