By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,869 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,869 IT Pros & Developers. It's quick & easy.

Setting PYTHONPATH from Makefile

P: n/a
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 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
[pi************@gmail.com]
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
Dec 2 '05 #2

P: n/a
"pi************@gmail.com" <pi************@gmail.com> 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 3 '05 #3

P: n/a
On Fri, Dec 02, 2005 at 07:33:20PM -0500, Mike Meyer wrote:
"pi************@gmail.com" <pi************@gmail.com> 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
Dec 3 '05 #4

P: n/a
Inyeol Lee <in********@siliconimage.com> 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 3 '05 #5

P: n/a
On Fri, Dec 02, 2005 at 08:10:41PM -0500, Mike Meyer wrote:
Inyeol Lee <in********@siliconimage.com> 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
Dec 3 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.