Python 'cannot import name'

2019-08-31 05:38发布

问题:

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.