Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; '21,': 0.07; 'attribute': 0.07; 'subject:file': 0.07; 'subject:into': 0.09; 'subject:module': 0.09; "'__doc__',": 0.16; "'__file__',": 0.16; '-tkc': 0.16; '>on': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'imports': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; 'module': 0.19; 'passing': 0.19; 'work,': 0.20; '>>>': 0.22; 'import': 0.22; 'print': 0.22; '31,': 0.24; 'subject: .': 0.24; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'work.': 0.31; "skip:' 10": 0.31; 'file': 0.32; '(most': 0.33; 'but': 0.35; 'charset:us-ascii': 0.36; 'january': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; "you're": 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'imp': 0.84; 'received:50.22': 0.84 Date: Tue, 21 Jan 2014 09:36:20 -0600 From: Tim Chase To: python-list@python.org Subject: Re: import file without .py into another module In-Reply-To: <022c00a5-87ee-4b2a-a6f6-e23c8bda1e12@googlegroups.com> References: <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> <022c00a5-87ee-4b2a-a6f6-e23c8bda1e12@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390318534 news.xs4all.nl 2897 [2001:888:2000:d::a6]:37874 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64422 On 2014-01-21 07:13, kevinbercaw@gmail.com wrote: >On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote: >> configModuleObject = imp.load_source(fileName, filePath) >> >> imports the module and then binds it to the name >> configModuleObject, >> >> therefore: >> >> print configModuleObject.myVar > > Yep, I tried that right off as that's how I thought it would work, > but it doesn't work. Traceback (most recent call last): > File "mainScript.py", line 31, in > print configModuleObject.myVar > AttributeError: 'module' object has no attribute 'myVar' Check what you're passing for fileName/FilePath: >>> import imp >>> with open('demo.txt', 'wb') as f: ... f.write('x = 42\ny = "hello"\n') ... >>> d = imp.load_source('SomeName', 'demo.txt') >>> dir(d) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'x', 'y'] >>> d.x 42 >>> d.y 'hello' >>> d.__name__ 'SomeName' -tkc