AttributeError:'module'对象没有由* .pyc引起的属性'join'

时间:2016-12-06 22:42:58

标签: python pyc

我在mac 1行文件上创建了:  myfile.py

import networkx

我明白了:

    ['Apricot', 'Aquamarine', 'Bittersweet', 'Black', 'Blue', 'BlueGreen', 'BlueViolet', 'BrickRed', 'Brown', 'BurntOrange', 'CadetBlue', 'CarnationPink', 'Cerulean', 'CornflowerBlue', 'Cyan', 'Dandelion', 'DarkOrchid', 'Emerald', 'ForestGreen', 'Fuchsia', 'Goldenrod', 'Gray', 'Green', 'GreenYellow', 'JungleGreen', 'Lavender', 'LimeGreen', 'Magenta', 'Mahogany', 'Maroon', 'Melon', 'MidnightBlue', 'Mulberry', 'NavyBlue', 'OliveGreen', 'Orange', 'OrangeRed', 'OrchidPeach', 'Periwinkle', 'PineGreen', 'Plum', 'ProcessBlue', 'Purple', 'RawSienna', 'Red', 'RedOrange', 'RedViolet', 'Rhodamine', 'RoyalBlue', 'RoyalPurple', 'RubineRed', 'Salmon', 'SeaGreen', 'Sepia', 'SkyBlue', 'SpringGreen', 'Tan', 'TealBlue', 'Thistle', 'Turquoise', 'Violet', 'VioletRed', 'WhiteWild', 'Strawberry', 'Yellow', 'YellowGreen', 'YellowOrange']
Traceback (most recent call last):
  File "myfile.py", line 1, in <module>
    import networkx
  File "/Library/Python/2.7/site-packages/networkx/__init__.py", line 70, in <module>
    import networkx.classes
  File "/Library/Python/2.7/site-packages/networkx/classes/__init__.py", line 7, in <module>
    from .function import *
  File "/Library/Python/2.7/site-packages/networkx/classes/function.py", line 509, in <module>
    @not_implemented_for('directed')
  File "<decorator-gen-1>", line 2, in _not_implemented_for
  File "/Library/Python/2.7/site-packages/decorator.py", line 232, in decorate
    evaldict, __wrapped__=func)
  File "/Library/Python/2.7/site-packages/decorator.py", line 219, in create
    self = cls(func, name, signature, defaults, doc, module)
  File "/Library/Python/2.7/site-packages/decorator.py", line 115, in __init__
    formatvalue=lambda val: "", *argspec)[1:-1])
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 869, in formatargspec
    return '(' + string.join(specs, ', ') + ')'
AttributeError: 'module' object has no attribute 'join'

我通过终端做到了,它起作用了:

    :~ yev$ python
Python 2.7.12 (default, Oct 11 2016, 05:24:00) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import networkx
>>> print 'test'
test
>>> G = networkx.barabasi_albert_graph(2,1,1)
>>> print G.degree().values()
[1, 1]
>>> 

我再次做了之后,它没有用。 最后,我意识到问题出现在下面:我从不同的目录执行导入networkx(通常我在桌面上工作),直到我删除我之前创建的Desktop中的测试文件时才能工作(由我)。该文件的名称为:

  

string.pyc

因此问题是:发生了什么。为什么这个* .pyc文件导致了问题。是否所有* .pyc文件都会产生此类问题?

1 个答案:

答案 0 :(得分:1)

*.pyc文件是您的代码的编译版本。这是python vm执行的字节码。如果您的*.py文件发生更改,python会将*.pyc文件替换为更新的文件。

在您的情况下,看起来您的string.py文件已在您正在使用的某个文件夹中编译为string.pyc。此文件隐藏了标准字符串模块(项目的主目录始终首先查找)。 这使inspect.py模块导入了string.py而不是标准的lib。并且存在错误。