Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64989 > unrolled thread
| Started by | loial <jldunn2000@gmail.com> |
|---|---|
| First post | 2014-01-30 04:39 -0800 |
| Last post | 2014-01-30 06:26 -0800 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | loial <jldunn2000@gmail.com> |
|---|---|
| Date | 2014-01-30 04:39 -0800 |
| Subject | Add 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]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2014-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]
| From | loial <jldunn2000@gmail.com> |
|---|---|
| Date | 2014-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]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-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]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2014-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]
| From | loial <jldunn2000@gmail.com> |
|---|---|
| Date | 2014-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