Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83303

Re: pickle, modules, and ImportErrors

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'package,': 0.03; 'causing': 0.04; 'importing': 0.05; 'correct.': 0.07; 'definitions': 0.07; 'failing': 0.07; 'fixes': 0.07; 'importerror:': 0.07; 'paths': 0.07; 'remaining': 0.07; 'result,': 0.07; 'loaded,': 0.09; 'statements': 0.09; 'subject:modules': 0.09; 'try:': 0.09; 'python': 0.11; 'jan': 0.12; 'changes': 0.15; '"from': 0.16; 'importerror': 0.16; 'imports': 0.16; 'likewise': 0.16; 'script,': 0.16; 'sys.path': 0.16; 'then?': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'trying': 0.19; 'later': 0.20; 'import': 0.22; 'separate': 0.22; 'adds': 0.24; 'circular': 0.24; 'instead.': 0.24; 'module,': 0.24; 'package.': 0.24; 'script.': 0.24; 'specify': 0.24; 'earlier': 0.24; 'fine': 0.24; 'file.': 0.24; 'source': 0.25; 'script': 0.25; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'appear': 0.29; 'correct': 0.29; 'external': 0.29; "doesn't": 0.30; 'absolute': 0.30; 'important.': 0.30; 'relative': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'loading': 0.31; 'names.': 0.31; 'pickle': 0.31; 'file': 0.32; 'front': 0.32; 'run': 0.32; 'running': 0.33; 'problem': 0.35; "can't": 0.35; 'created': 0.35; 'except': 0.35; 'problem.': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'executing': 0.36; 'i.e.': 0.36; 'module.': 0.36; 'two': 0.37; 'list.': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'problems.': 0.60; 'john': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'making': 0.63; 'real': 0.63; 'such': 0.63; 'myself': 0.63; 'refer': 0.63; 'different': 0.65; 'needing': 0.65; 'within': 0.65; 'containing': 0.69; 'below.': 0.71; '2015': 0.84; 'confusing': 0.84; 'confusion.': 0.84; 'presumably': 0.84; 'usage.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=NTw6CZXerLgxIRXBsgUZV8wpGpEA6COhZuYQO8XiEqc=; b=JAQYpRGbrJcX+YXtsUGLDx1qIMpa63/cVLTUpl7Y/Zlfxvu2pOezJkaWlNEzeRg3zr Eog/lQfjOzMK3gckpJ6G+EGmbKO/UOEmJNSPBMjnHpHwMrcRX9xL3+Q0Dx1VKNXJdzQd 3CXooKRdRA00H4goY+17qrV2WZ2QKv7WDe0qej2+Tj71pIf9nIhF4W67Zoh4PnddbXXj gr21+lZ3bsaGlMRa6OS4L17fl5sHSrJAs4haw2km9HOtWd9kOAgZm5PnDG7lyvdcyUja szus7wFe9dGNQlMJ5bMDythra9MgLuR2vzlan7ZP1Z8OXQjRHCP4h7h7F8vb3fL1TisF +OhA==
X-Received by 10.68.130.4 with SMTP id oa4mr8768653pbb.95.1420664516890; Wed, 07 Jan 2015 13:01:56 -0800 (PST)
MIME-Version 1.0
In-Reply-To <eb5918fb-41ea-4199-b2f2-ae28b454c983@googlegroups.com>
References <eb5918fb-41ea-4199-b2f2-ae28b454c983@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 7 Jan 2015 14:01:16 -0700
Subject Re: pickle, modules, and ImportErrors
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
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.17454.1420664918.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420664918 news.xs4all.nl 2874 [2001:888:2000:d::a6]:33880
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83303

Show key headers only | View raw


On Wed, Jan 7, 2015 at 1:12 PM, John Ladasky <john_ladasky@sbcglobal.net> wrote:
> Do I need to "import my_svr.model as model" then?  Adding that line changes nothing. I get the exact same "ImportError: No module named 'model'".
>
> Likewise for "import my_svr", "from my_svr import *", or even "from my_svr.model import SVRModel".
>
> It is clear that I'm failing to understand something important.
>
> I do not have any circular import dependencies; however, some of the files in my package do need to import definitions from files earlier in my data pipeline.  In order to make everything work inside the module, as well as making a parent-folder "import my_svr" work from a iPython,  I find myself needing to use statements like these inside my training.py program:
>
>
> try:
>     from model import *
>     from sampling import *
> except ImportError:
>     from .model import *
>     from .sampling import *
>
>
> This bothers me.  I don't know whether it is correct usage.  I don't know whether it is causing my remaining ImportError problem.

It sounds like you have import path confusion. The first imports there
are absolute imports. If the absolute paths of the named modules are
just "model" and "sampling", then that would be correct. However, it
sounds like these modules are part of a "my_svr" package, so the
absolute paths of these modules are actually "my_svr.model" and
"my_svr.sampling". In that case, the the absolute path that you're
trying to use for the import is wrong. Either fully specify the module
path, or use the relative import you have below.

Now, why might those absolute imports appear to work even though the
paths are incorrect? Well, it sounds like you may be running a file
inside the package as your main script. This causes some problems.

1) Python doesn't realize that the script it's running is part of a
package. It just calls that module '__main__', and if something else
happens to import the module later by its real name, then you'll end
up loading a second copy of the module by that name, which can lead to
confusing behavior.

2) Python implicitly adds the directory containing the script to the
front of the sys.path list. Since the directory containing the package
is presumably already on the sys.path, this means that the internal
modules now appear in sys.path *twice*, with two different names:
"my_svr.model" and "model" refer to the same source file. Python
doesn't realize this however, and so they don't refer to the same
module. As a result, importing both "my_svr.model" and "model" will
again result in two separate copies of the module being loaded, with
two different names.

It sounds like your pickle file was created from an object from the
"model" module. This works fine when the sys.path is set such that
"model" is the absolute path of a module, i.e. when you run your main
script within the package. When running an external main script,
sys.path doesn't get set that way, and so it can't find the "model"
module.

The solution to this is to avoid executing a module within a package
as your main script. Run a script outside the package and have it
import and call into the package instead. An alternative is to run the
module using the -m flag of the Python executable; this fixes problem
2 above but not problem 1.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

pickle, modules, and ImportErrors John Ladasky <john_ladasky@sbcglobal.net> - 2015-01-07 12:12 -0800
  Re: pickle, modules, and ImportErrors Devin Jeanpierre <jeanpierreda@gmail.com> - 2015-01-07 14:55 -0600
    Re: pickle, modules, and ImportErrors John Ladasky <john_ladasky@sbcglobal.net> - 2015-01-07 16:23 -0800
      Re: pickle, modules, and ImportErrors Chris Angelico <rosuav@gmail.com> - 2015-01-08 12:06 +1100
  Re: pickle, modules, and ImportErrors Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-07 14:01 -0700

csiph-web