Python 'cannot import name'

2019-08-31 05:28发布

So i have structure like this

enter image description here

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条回答
何必那么认真
2楼-- · 2019-08-31 06:13

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.

查看更多
登录 后发表回答