我在 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]
>>>
我又做了一次之后,还是不行。最后,我意识到问题出在以下几点:我正在从不同的目录执行 import networkx (通常我从桌面工作),直到我删除了我在桌面中创建的测试文件之前它没有工作(由我创建) )。该文件的名称为:
字符串.pyc
因此问题是:发生了什么。为什么这个 *.pyc 文件会导致问题。是否所有 *.pyc 文件都会产生此类问题?