简体   繁体   English

有关python的更多信息ImportError没有命名的模块

[英]More on python ImportError No module named

Following the suggestion here , my package (or the directory containing my modules) is located at C:/Python34/Lib/site-packages. 根据这里的建议,我的包(或包含我的模块的目录)位于C:/ Python34 / Lib / site-packages。 The directory contains an __init__.py and sys.path contains a path to the directory as shown. 该目录包含__init__.pysys.path包含目录的路径,如图所示。

在此输入图像描述

Still I am getting the following error: 我仍然收到以下错误:

Traceback (most recent call last):
  File "C:/Python34/Lib/site-packages/toolkit/window.py", line 6, in <module>
    from catalogmaker import Catalog
  File "C:\Python34\Lib\site-packages\toolkit\catalogmaker.py", line 1, in <module>
    from patronmaker import Patron
  File "C:\Python34\Lib\site-packages\toolkit\patronmaker.py", line 4, in <module>
    class Patron:
  File "C:\Python34\Lib\site-packages\toolkit\patronmaker.py", line 11, in Patron
    patrons = pickle.load(f)
ImportError: No module named 'Patron'

I have a class in patronmaker.py named 'Patron' but no module named Patron so I am not sure what the last statement in the error message means. 我在patronmaker.py中有一个名为'Patron'但没有名为Patron的模块,所以我不确定错误信息中的最后一个语句是什么意思。 I very much appreciate your thoughts on what I am missing. 我非常感谢你对我失踪的想法。

Python Version 3.4.1 on a Windows 32 bits machine. Windows 32位计算机上的Python 3.4.1版

You are saving all patron instances (ie self ) to the Patron class attribute Patron.patrons . 您将所有patron实例(即self )保存到Patron类属性Patron.patrons Then you are trying to pickle a class attribute from within the class. 然后你试图从类中挑选一个类属性。 This can choke pickle , however I believe dill should be able to handle it. 这可以扼杀pickle ,但我相信dill应该能够处理它。 Is it really necessary to save all the class instances to a list in Patrons? 是否真的有必要将所有类实例保存到Patrons中的列表中? It's a bit of an odd thing to do… 这有点奇怪......

pickle serializes classes by reference, and doesn't play well with __main__ for many objects. pickle通过引用序列化类,并且对于许多对象而言不适合__main__ In dill , you don't have to serialize classes by reference, and it can handle issues with __main__ , much better. dill ,您不必通过引用序列化类,它可以更好地处理__main__问题。 Get dill here: https://github.com/uqfoundation 在这里获取dillhttps//github.com/uqfoundation

Edit: I tried your code (with one minor change) and it worked. 编辑:我尝试了你的代码(只有一个小的改动),它工作。

dude@hilbert>$ python patronmaker.py

Then start python… 然后启动python ......

>>> import dill
>>> f = open('patrons.pkl', 'rb')
>>> p = dill.load(f)
>>> p
[Julius Caeser, Kunte Kinta, Norton Henrich, Mother Teresa]

The only change I made was to uncomment the lines at the end of patronmaker.py so that it saved some patrons…. 我做的唯一改变是在patronmaker.py结束时取消注释,以便它节省了一些顾客....... and I also replaced import pickle with import dill as pickle everywhere. 而且我还用import dill as pickle取代import pickle import dill as pickle到处都是。

So, even by downloading and running your code, I can't produce an error with dill . 因此,即使下载并运行您的代码,我也不能用dill产生错误。 I'm using the latest dill from github. 我正在使用github的最新dill

Additional Edit: Your traceback above is from an ImportError . 附加编辑:上面的回溯来自ImportError Did you install your module? 你安装了模块吗? If you didn't use setup.py to install it, or if you don't have your module on your PYTHONPATH , then you won't find your module regardless of how you are serializing things. 如果您没有使用setup.py进行安装,或者您的PYTHONPATH上没有模块,那么无论您如何序列化,都无法找到模块。

Even more edits: Looking at your code, you should be using the singleton pattern for patrons … it should not be inside the class Patron . 更多编辑:看看你的代码,你应该为patrons使用单身模式 ......它不应该在class Patron The block of code at the class level to load the patrons into Patron.patrons is sure to cause problems… and probably bound to be the source of some form of errors. 类级别的代码块将顾客加载到Patron.patrons肯定会引起问题......并且可能必然会成为某种形式的错误的根源。 I also see that you are pickling the attribute Patrons.patrons (not even the class itself) from inside the Patrons class -- this is madness -- don't do it. 我也看到你酸洗属性Patrons.patrons从内部(甚至不是类本身) Patrons类- 这太疯狂了 -不这样做。 Also notice that when you are trying to obtain the patrons, you use Patron.patrons … this is calling the class object and not an instance. 另请注意,当您尝试获取顾客时,您使用Patron.patrons ...这是调用类对象而不是实例。 Move patrons outside of the class, and use the singleton directly as a list of patrons. 将顾客移到课堂外,并直接使用单身人士作为顾客列表。 Also you should typically be using the patrons instance, so if you wanted to have each patron know who all the other patrons are, p = Patron('Joe', 'Blow') , then p.patrons to get all patrons… but you'd need to write a Patrons.load method that reads the singleton list of patrons… you could also use a property to make the load give you something that looks like an attribute. 另外你通常应该使用顾客实​​例,所以如果你想让每个顾客都知道所有其他顾客是谁, p = Patron('Joe', 'Blow') ,然后p.patrons来获得所有顾客......但是你我需要编写一个Patrons.load方法来读取单个顾客列表...你也可以使用一个property来使load给你一些看起来像属性的东西。

If you build a singleton of patrons (as a list)… or a "registry" of patrons (as a dict) if you like, then just check if a patrons pickle file exists… to load to the registry… and don't do it from inside the Patrons class… things should go much better. 如果您构建一个单独的顾客(作为列表)...或者顾客的“注册表”(作为dict),如果您愿意,那么只需检查是否存在顾客pickle文件...加载到注册表...并且不要做它来自Patrons课程......事情应该会好得多。 Your code currently is trying to load a class instance on a class definition while it builds that class object. 您的代码当前正在尝试在构建该类对象时在类定义上加载类实例。 That's bad... 那很糟...

Also, don't expect people to go downloading your code and debugging it for you, when you don't present a minimal test case or sufficient info for how the traceback was created. 此外,当您没有提供最小的测试用例或有关如何创建回溯的足够信息时,不要指望人们下载您的代码并为您调试它。 You may have hit on a valid pickling error in dill for some dark corner case, but I can't tell b/c I can't reproduce your error. 对于某些暗角情况,您可能dill遇到了有效的酸洗错误,但我无法告诉b / c我无法重现您的错误。 However, I can tell that you need some refactoring. 但是,我可以说你需要一些重构。

And just to be explicit: 而且要明确:

Move your patrons initializing mess from Patrons into a new file patrons.py 将您的顾客从patrons.py初始化混乱转移到新文件patrons.py

import os
import dill as pickle

#Initialize patrons with saved pickle data
if os.path.isfile('patrons.pkl'):
    with open("patrons.pkl", 'rb') as f:
        patrons = pickle.load(f)
else: patrons = []

Then in patronmaker.py, and everywhere else you need the singleton… 然后在patronmaker.py和其他地方你需要单身...

import dill as pickle
import os.path
import patrons as the

class Patron:

    def __init__(self, lname, fname):
        self.lname = lname.title()
        self.fname = fname.title()
        self.terrCheckedOutHistory = {}
        #Add any created Patron to patrons list
        the.patrons.append(self)
        #Preserve this person via pickle
        with open('patrons.pkl', 'wb') as f:
            pickle.dump(the.patrons, f)

And you should be fine unless your code is hitting one of the cases that attributes on modules can't be serialized because they were added dynamically (see https://github.com/uqfoundation/dill/pull/47 ), which should definitely make pickle fail, and in some cases dill too… probably with an AtrributeError on the module. 你应该没问题,除非你的代码遇到一种情况,模块上的属性无法被序列化,因为它们是动态添加的(参见https://github.com/uqfoundation/dill/pull/47 ),这应该是肯定的使pickle失败,在某些情况下也会dill ...可能在模块上有AtrributeError I just can't reproduce this… and I'm done. 我只是无法重现这一点......我已经完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM