Python 'cannot import name'

邮差的信 提交于 2020-01-06 13:52:22

问题


So i have structure like this

And i want to import userlist.py from server.py. Server py:

import socket

from server import userlist #<-- error here

class Server():
    def __init__(self,port):

Other files in server don't have any refferences to userlist. It says cannot import name userlist

If i change import to

from server.userlist import Userlist

it says: 'server' is not a package


回答1:


Try just

import userlist

When you are importing a file (module) in the same directory, all you need to do is import the name of that file without the .py part.



来源:https://stackoverflow.com/questions/31009668/python-cannot-import-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!