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


Groups > comp.lang.python > #83302

Re: pickle, modules, and ImportErrors

Path csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jeanpierreda@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; 'causing': 0.04; 'importing': 0.05; 'definitions': 0.07; 'failing': 0.07; 'importerror:': 0.07; 'remaining': 0.07; '*is*': 0.09; 'differing': 0.09; 'exec': 0.09; 'here?': 0.09; 'referenced': 0.09; 'statements': 0.09; 'subject:modules': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; 'changes': 0.15; '"from': 0.16; '(note': 0.16; 'fails.': 0.16; 'file):': 0.16; 'importerror': 0.16; 'imports': 0.16; 'interpreter,': 0.16; 'likewise': 0.16; 'modules,': 0.16; 'then?': 0.16; 'top-level': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'code,': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'circular': 0.24; 'module,': 0.24; 'necessary.': 0.24; 'package.': 0.24; 'earlier': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'second': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'important.': 0.30; 'relative': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'directly,': 0.31; 'fine,': 0.31; 'file': 0.32; 'figure': 0.32; 'run': 0.32; '(most': 0.33; 'except': 0.35; 'problem.': 0.35; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'module.': 0.36; 'should': 0.36; 'example,': 0.37; 'level': 0.37; 'clear': 0.37; 'files': 0.38; 'pm,': 0.38; 'recent': 0.39; 'even': 0.60; 'hope': 0.61; 'john': 0.61; 'first': 0.61; 'making': 0.63; 'myself': 0.63; 'refer': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'needing': 0.65; 'p.s.': 0.66; '2015': 0.84; 'blob': 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 :cc:content-type:content-transfer-encoding; bh=Ak7S1GGFgvrlUiqXznFreQ9uZc7hyIIIf1VgNK8gI48=; b=pJ0kDEC3S4Ho38xlxmEf5f/4qo5QTHV7GgkodSzU57zno7TWBL8SrIbZ4JwBfTdIuc XIbwukJpTB+lii4TYw0Q5TzK2bXybKTClPIVWCcSa09cb6/iIKXvBA6LY3Xgxkfx49Ck 2qCV10AjKaIVcB1RmyRS30RMPDPmYctVRVqHOLH+c0YVqEz8D18X9aeuEFqXz1ugYBo5 5TgEHIhAih+JDAzE5YIppF1cozp5tD28xN5ZAHcsOXFXu5IEPiRHvV/4lgAqenA6HwqQ hqwsyHi0zdM3NpwsQrJJanvtZaEcaladTFblNXrl0mLPIvbNyqs4MvkxGvn8NSwJ6pNt gLTw==
X-Received by 10.224.129.132 with SMTP id o4mr8146729qas.66.1420664173096; Wed, 07 Jan 2015 12:56:13 -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 Devin Jeanpierre <jeanpierreda@gmail.com>
Date Wed, 7 Jan 2015 14:55:32 -0600
Subject Re: pickle, modules, and ImportErrors
To John Ladasky <john_ladasky@sbcglobal.net>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc "comp.lang.python" <python-list@python.org>
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.17453.1420664176.18130.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420664176 news.xs4all.nl 2893 [2001:888:2000:d::a6]:58149
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83302

Show key headers only | View raw


On Wed, Jan 7, 2015 at 2:12 PM, John Ladasky <john_ladasky@sbcglobal.net> wrote:
> If I execute "import my_svr" in an iPython interpreter, everything works as I think that I should expect:
-snip-
> However, a nearly-identical program in the parent folder fails (note that all I change is the relative path to the file):
> Traceback (most recent call last):
>   File "reload test different directory.py", line 6, in <module>
>     model = load(f)
> ImportError: No module named 'model'
>
>
> 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.

in the first case, the model module was available as a top-level
module, "model". Pickles referenced that module when serialized. In
the second case, the model module was available as a submodule of the
top level my_svr package. So any pickles serialized from there would
use my_svr.model to refer to the model module. There *is* no model
module in this second case, so deserializing fails.

If you never run model directly, and only ever import it or run it as
my_svr.model, then you will be fine, and pickles will all serialize
and deserialize the same way.

For example, instead of python -i my_svr/model.py, you can use python
-im my_svr.model . (or ipython -im my_svr.model).

P.S. don't use pickle, it is a security vulnerability equivalent in
severity to using exec in your code, and an unversioned opaque
schemaless blob that is very difficult to work with when circumstances
change.

> 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.

This is a symptom of the differing ways you are importing these
modules, as above. If you only ever run them and import them as
my_svr.blahblah, then only the second set of imports are necessary.

P.S. don't use import *, and if you do use import *, don't use more
than one per file -- it makes it really hard to figure out where a
given global came from (was it defined here? was it defined in model?
was it defined in sampling?)

I hope that resolves all your questions!

-- Devin

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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