Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65008
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <mail@timgolden.me.uk> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'importing': 0.05; 'dynamically': 0.07; 'sys': 0.07; 'os.path': 0.09; 'skip:o 60': 0.09; 'undefined': 0.09; 'python': 0.11; '6th': 0.16; 'fails.': 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'i.e': 0.16; 'line).': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:Add': 0.16; 'subject:based': 0.16; 'subject:variable': 0.16; 'sys.path': 0.16; 'tjg': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'help.': 0.21; 'import': 0.22; 'header:User- Agent:1': 0.23; 'received:192.168.100': 0.24; 'script.': 0.24; 'fine': 0.24; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'generally': 0.29; 'relative': 0.30; 'code': 0.31; '(since': 0.31; 'os,': 0.31; 'there,': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'executing': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'free': 0.61; 'back': 0.62; "you've": 0.63; 'more': 0.64; 'here': 0.66; 'below.': 0.71; 'safe': 0.72; 'from:addr:mail': 0.83; 'around,': 0.84; 'presumably': 0.84; 'scatter': 0.84; 'imagine': 0.93 |
| Date | Thu, 30 Jan 2014 14:14:18 +0000 |
| From | Tim Golden <mail@timgolden.me.uk> |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Add directory to sys.path based on variable |
| References | <08cb4673-c926-487e-a101-299ed899239a@googlegroups.com> <mailman.6128.1391085772.18130.python-list@python.org> <7fcdfee3-c52a-4840-8033-920d23d3d7e3@googlegroups.com> |
| In-Reply-To | <7fcdfee3-c52a-4840-8033-920d23d3d7e3@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6142.1391091261.18130.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391091261 news.xs4all.nl 2855 [2001:888:2000:d::a6]:45113 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65008 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web