简体   繁体   中英

Cannot import Python module after naming my own file the same name as the module

I am using the Python module called census . I accidentally named a file census.py and in that same file I added from census import Census .

I ran the census.py script locally, and received the following:

Traceback (most recent call last):
  File "acs5/census.py", line 9, in <module>
    from census import Census
  File "/Users/.../census.py", line 9, in <module>
ImportError: cannot import name Census

My mistake was naming the file census.py when I already have a census module in scope creating some kind of circular reference. I've since tried renaming the file and that didn't work. I removed and reinstalled census with pip and that didn't work either. I've also inspected the syspath and didn't see anything obvious that's wrong.

The project is also under source control with git, and I'm afraid that instead of using git mv old_name new_name I just renamed the files in my IDE. Maybe the git logs are out of wack... Not sure about this either and I've been investigating if git might be involved.

Stupidly, I thought that whatever local issue I had on my MacBook wouldn't exist on the remote Ubuntu server. So I ran the script on the server and received the same error. So now I'm stuck with two mess ups... Anyway, locally I have MacOS Anaconda2 and the remote server is Ubuntu's Python 2.7 install.

Can somebody please help me resolve my module issues?

I figured it out ... I had to remove census.pyc from my project. Crisis averted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM