Add directory to sys.path based on variable

L

loial

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
 
T

Tim Golden

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
 
L

loial

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 *
 
C

Chris Angelico

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
 
T

Tim Golden

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
 
L

loial

Idiot that I am...I was not calling the script with the full path !

Thanks for your help
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top