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: 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 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> <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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