Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64989 > unrolled thread

Add directory to sys.path based on variable

Started byloial <jldunn2000@gmail.com>
First post2014-01-30 04:39 -0800
Last post2014-01-30 06:26 -0800
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Add directory to sys.path based on variable loial <jldunn2000@gmail.com> - 2014-01-30 04:39 -0800
    Re: Add directory to sys.path based on variable Tim Golden <mail@timgolden.me.uk> - 2014-01-30 12:42 +0000
      Re: Add directory to sys.path based on variable loial <jldunn2000@gmail.com> - 2014-01-30 06:03 -0800
        Re: Add directory to sys.path based on variable Chris Angelico <rosuav@gmail.com> - 2014-01-31 01:09 +1100
        Re: Add directory to sys.path based on variable Tim Golden <mail@timgolden.me.uk> - 2014-01-30 14:14 +0000
          Re: Add directory to sys.path based on variable loial <jldunn2000@gmail.com> - 2014-01-30 06:26 -0800

#64989 — Add directory to sys.path based on variable

Fromloial <jldunn2000@gmail.com>
Date2014-01-30 04:39 -0800
SubjectAdd directory to sys.path based on variable
Message-ID<08cb4673-c926-487e-a101-299ed899239a@googlegroups.com>
I want to add a path to sys.path based upon a variable

Can I do that?

Hardcodeing the path works fine, but I want to set it based upon a variable.

I know I can set PYTHONPATH, but just wondering if I can add a directory on the fly to sys.path using a variable




[toc] | [next] | [standalone]


#64990

FromTim Golden <mail@timgolden.me.uk>
Date2014-01-30 12:42 +0000
Message-ID<mailman.6128.1391085772.18130.python-list@python.org>
In reply to#64989
On 30/01/2014 12:39, loial wrote:
> I want to add a path to sys.path based upon a variable
> 
> Can I do that?
> 
> Hardcodeing the path works fine, but I want to set it based upon a
> variable.
> 
> I know I can set PYTHONPATH, but just wondering if I can add a
> directory on the fly to sys.path using a variable

Certainly:

<code>
import sys

newpath = "c:/users/tim/modules"
sys.path.append(newpath)

</code>


TJG

[toc] | [prev] | [next] | [standalone]


#65004

Fromloial <jldunn2000@gmail.com>
Date2014-01-30 06:03 -0800
Message-ID<7fcdfee3-c52a-4840-8033-920d23d3d7e3@googlegroups.com>
In reply to#64990
Ok, that works fine with the apth hard coded, but I want to do something like the code below. i.e I am trying to dynamically add a path that is relative to the path of the current executing python script.

In this case the import fails.

import sys
import os
from os.path import *

scriptpath=os.path.dirname(sys.argv[0])
otherscriptspath=printerpath.replace("scripts","otherscripts")
sys.path.append(otherscriptspath)

from AuditUpdate import *


[toc] | [prev] | [next] | [standalone]


#65006

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 01:09 +1100
Message-ID<mailman.6140.1391091007.18130.python-list@python.org>
In reply to#65004
On Fri, Jan 31, 2014 at 1:03 AM, loial <jldunn2000@gmail.com> wrote:
> In this case the import fails.
>
> import sys
> import os
> from os.path import *

Not sure why you need that, since you're explicitly naming
"os.path.dirname". The base "import os" shoul cover that for you.

> scriptpath=os.path.dirname(sys.argv[0])
> otherscriptspath=printerpath.replace("scripts","otherscripts")
> sys.path.append(otherscriptspath)

Try adding here:

print(sys.argv[0])
print(otherscriptspath)

See what they tell you. Is sys.argv[0] what you think it is? Is the
resulting path what you expect?

ChrisA

[toc] | [prev] | [next] | [standalone]


#65008

FromTim Golden <mail@timgolden.me.uk>
Date2014-01-30 14:14 +0000
Message-ID<mailman.6142.1391091261.18130.python-list@python.org>
In reply to#65004
On 30/01/2014 14:03, loial wrote:
> Ok, that works fine with the apth hard coded, but I want to do something like the code below. i.e I am trying to dynamically add a path that is relative to the path of the current executing python script.
> 
> In this case the import fails.
> 
> import sys
> import os
> from os.path import *
> 
> scriptpath=os.path.dirname(sys.argv[0])
> otherscriptspath=printerpath.replace("scripts","otherscripts")
> sys.path.append(otherscriptspath)
> 
> from AuditUpdate import *

Well, adding a path to sys.path and then importing is generally
considered a safe bet. So it's more likely that somewhere inside your
path manipulation something's going wrong.

You've presumably not cut-and-pasted that code from the console (since
you've got an undefined "printerparth" on the 6th line). But why not
scatter some print()s and os.listdir()s around, eg:


import os, sys

print(sys.path)
print(sys.argv[0])

scriptpath=os.path.dirname(sys.argv[0])
print(scriptpath)

otherscriptpath = scriptpath.replace("xxx", "yyy")
print(otherscriptpath)
print(os.listdir(otherscriptpath))

... and so on. Somewhere in there, I imagine something won't be as you
expect. Feel free to come back here if you need more help.

TJG

[toc] | [prev] | [next] | [standalone]


#65010

Fromloial <jldunn2000@gmail.com>
Date2014-01-30 06:26 -0800
Message-ID<5c7a3ef4-3048-47a7-81a1-41bad721f77c@googlegroups.com>
In reply to#65008
Idiot that I am...I was not calling the script with the full path !

Thanks for your help

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web