Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'defines': 0.07; 'turtle': 0.07; 'python': 0.09; 'callable': 0.09; 'other)': 0.09; 'sep': 0.09; 'subject:modules': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'language': 0.14; 'file,': 0.15; '"import': 0.16; "'__doc__',": 0.16; "'__file__',": 0.16; '.py': 0.16; '2.7.3': 0.16; 'doing,': 0.16; 'foo"': 0.16; 'foo()': 0.16; 'foo(object):': 0.16; 'instantiate': 0.16; 'java.': 0.16; 'math,': 0.16; 'subject:import': 0.16; 'wrote:': 0.17; 'module,': 0.17; '>>>': 0.18; 'putting': 0.20; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'class.': 0.23; "haven't": 0.23; "i've": 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'thanks!': 0.26; '(most': 0.27; 'possibly': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'skip:( 20': 0.28; '3.1': 0.29; 'cat': 0.29; 'objects': 0.29; 'class': 0.29; "i'm": 0.29; '(including': 0.30; "skip:' 10": 0.30; 'received:209.85.215.46': 0.30; 'error': 0.30; 'compatible': 0.30; '(and': 0.32; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'apple': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'easy': 0.60; 'save': 0.61; 'first': 0.61; '30,': 0.62; 'more': 0.63; 'making': 0.64; 'hello!': 0.65; '4.2.1': 0.84; '"one': 0.91; 'on?': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:x-gm-message-state; bh=KIb156ycq/LqAXzOA3P5Ii1iX562cEDFr9Sck/CzwMk=; b=G7q/uloGJ9Sal8ULXtbAo4MykCJIb+xabIa2WEzEZw5ohV4zS77u3/MA3X3kCx0r8s pjF8JCLlib4uOpwJPsn/vWCbH857etrqnocEXErNrWMQqCZMRdjP7HqXrSkSaBZLtcgU HQziWAFbwauEYs+56huMAsFL2MWfALz66dJSGXbFq7HoFT507ZwIXTpztWn8Ys3PfpjE DPMqj0wipcsCtNyMzQ3MRJiu52qm5iNNUUOtlHgRyH+xbNryx2Kv4J97IlONnVNmj2eG H410w03rzMjzzPJt9O074ThO5FLhYcNqRr4isd+Hj08B+O37HuIqVkRQvJ4UXwMrZeZk WNMg== MIME-Version: 1.0 In-Reply-To: <36813ff7-68b9-44eb-9b25-7c13ae739566@googlegroups.com> References: <36813ff7-68b9-44eb-9b25-7c13ae739566@googlegroups.com> Date: Sun, 30 Sep 2012 13:15:11 -0700 Subject: Re: Can't import modules From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQlzIjUPar1ioS/KXtWYNhe/beUPkI08oyE74E6HBp/qWY16hSEpejHpHf7qdIBmWXErIZhQ X-Junkmail-Whitelist: YES (by domain whitelist at mpv2.tis.cwru.edu) 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349036310 news.xs4all.nl 6864 [2001:888:2000:d::a6]:35034 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30566 On Sun, Sep 30, 2012 at 12:42 PM, Peter Farrell wrote: > Hello! > > I'm still new to Python, so here's another easy one. After I save something I've done as a .py file, how do I import it into something else I work on? Every time I try to import something other than turtle or math, I get this error message: > > 'module' object is not callable > > What am I doing wrong? > > Thanks! > > Peter Farrell > San Mateo, CA > -- Well, you haven't told us what you're doing, so it's hard to say what you're doing wrong. So I'm going to make a few guesses. 1. Your first (and possibly only other) language was Java. 2. You're making a class (let's say Foo) and putting it in a file of the same name (Foo.py) 3. You're doing "import Foo" and then calling "Foo()" trying to instantiate the class. Python doesn't have that "one class per file, and the file must have the same name as the class" rule as Java. The file defines a module, which is an object and can have any number of objects (including classes, because those are objects too) in it. $ cat foo.py class Foo(object): pass def add2(y) : return y + 2 bkaplan:~ bkaplan$ python Python 2.7.3 (default, Apr 23 2012, 10:06:17) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import foo >>> dir(foo) ['Foo', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add2', 'x'] >>> foo() Traceback (most recent call last): File "", line 1, in TypeError: 'module' object is not callable >>> foo.Foo() >>> foo.add2(5) 7