Setting PYTHONPATH from Makefile

P

pinkfloydhomer

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
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[[email protected]]
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.
 
M

Mike Meyer

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
 
I

Inyeol Lee

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


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
 
M

Mike Meyer

Inyeol Lee said:
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
 
I

Inyeol Lee

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top