473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Setting PYTHONPATH from Makefile

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
5 9822
[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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rami A. Kishek | last post by:
Hi, I would really appreciate help on this from all ye Win-oriented people. I have been using python under Linux for quite a while, know little about windows. I just upgraded my Python on WinME...
2
by: Eric Wichterich | last post by:
Hello Pythonistas, I am trying to get certain (self-written) libraries imported into my scripts using statements like "from library import function.py". But they are not being found. I...
4
by: r.e.s. | last post by:
I have no PYTHONPATH nor any other python-related environment variables, yet everything seems fine. (I'm using PythonWin with winxp.) As long as modules are loaded through PythonWin, is...
8
by: Tero Pihlajakoski | last post by:
Hi, I've been experimenting on embedding Python to a C software, and ran into a little problem with PYTHONPATH (I'm running on linux). Here's the deal: When trying to call...
3
by: D Denholm | last post by:
I recently installed Python 2.2 on my WinXP box. I am having problems figuring out how to create the PYTHONPATH correctly. I went to the WinXP SystemProperties > Advanced > Environment...
1
by: Neil Benn | last post by:
Hello, I'm trying to find some detailed documentation about PYTHONPATH, paths.pth, lib/site-packages, sitecustomise and so on. I know what they do but I'd like some detailed documentation about...
10
by: sushant.sirsikar | last post by:
Hi, I am using Linux env.I set the PYTHONPATH using import sys sys.path.append(----) But we i close python and start again i is not showing my new entry in PYTHONPATH. Can anyone help me to...
4
by: mhearne808[insert-at-sign-here]gmail[insert-dot-he | last post by:
I'm missing something major here. I'm trying to add a directory to my python path using the PYTHONPATH environment variable, and it's being ignored by the Python interactive shell. Below is a...
1
by: Aljosa Mohorovic | last post by:
i have a working MySQLdb module (/usr/lib/python2.4/site-packages/ MySQL_python-1.2.2-py2.4-linux-i686.egg), using it without problems. "clean shell" after login: python -c "import MySQLdb"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.