Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'interpreter': 0.05; 'importerror:': 0.07; 'subject:file': 0.07; 'python': 0.11; 'contents:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'knock': 0.16; 'pathname': 0.16; 'switch.': 0.16; 'syntaxerror:': 0.16; 'world!': 0.16; 'obviously': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; "aren't": 0.24; 'script.': 0.24; 'helpful': 0.24; "i've": 0.25; 'script': 0.25; 'post': 0.26; 'skip:_ 20': 0.27; 'absolute': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'easier': 0.31; 'file': 0.32; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'done': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'success.': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'manually': 0.60; 'different': 0.65; 'invalid': 0.68; 'how.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=nx6LfFI9VEe8G7lgPgjx0krBlWcj+lG3U+vG6favRvQ=; b=tKlvXTOTU65vIoGaTBJ6e+cMzdhxH17oU5NDCmGAqgUSyQkE7w1WSno59Pz149ymO4 Ge8Bj2DuUpTUtnjAFVDtTu/ZqSHNqMVRpoAEtdA1OzqGD4j/itaXPGWSMagAJd8jwxSL LV59IW3hD1l1Y9DtM/nGAVXvNgO5M8xKWZPfMUhG2dB1EGCSyp11tRaxO218dsBkbfEj h0ecAIoFAB3j3l5upEVaabYO7KMtNutY6rs6Cxl/NWTHd38lW3yKP9RVynXvOKKPVuKU extBqV9bqBQYxDBA07AstarX+oRsj9p0njfzxe2t4UXCZHwRbSYJugImtEroDzRpfOjB BYaA== MIME-Version: 1.0 X-Received: by 10.66.162.136 with SMTP id ya8mr20234043pab.110.1385264460961; Sat, 23 Nov 2013 19:41:00 -0800 (PST) Date: Sun, 24 Nov 2013 14:41:00 +1100 Subject: Importing by file name From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385264464 news.xs4all.nl 15925 [2001:888:2000:d::a6]:47866 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60348 As part of a post on python-ideas, I wanted to knock together a quick little script that "imports" a file based on its name, in the same way that the Python interpreter will happily take an absolute pathname for the main script. I'm sure there's a way to do it, but I don't know how. Obviously the import statement can't do it, but I've been poking around with __import__ and importlib without success. (Experiments are being done on Python 3.3; if a different version would make the job easier I'm happy to switch. This is just a curiosity tinkering.) Here's my current attempts (tracebacks chomped as they aren't very helpful here): >>> import "/x.py" SyntaxError: invalid syntax >>> importlib.import_module("/x.py") ImportError: No module named '/x' >>> importlib.import_module("/x") ImportError: No module named '/x' The best I can come up with is manually execing the file contents: >>> g={} >>> exec("def __main__():\n\tprint('Hello, world!')\n",g) >>> g["__main__"]() Hello, world! But that's not importing. Is there a way to do this as a module import? ChrisA