You are not logged in.
Hi,
I have a very peculiar problem. I cannot import a Python module from a certain directory, but from other directories it's OK. From problem directory, I start the python shell with
pythonand then inside the shell,
import mysql.connectorreturns with ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package. However, starting the python shell and executing the same import from any other directory is OK.
What could be going on? Please advise.
Last edited by doubleslash (2020-04-17 15:08:31)
Offline
What's in the directory where it fails?
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
There are some python source code files, but I think most suspicious is the directory __pycache__ with a file named mysql.cpython-38.pyc in it. I think this may be causing the problem and tried to remove it with
rm -r __pycache__, but it wasn't deleted. I even tried to do it with sudo, but that didn't help.
Offline
What do you mean it wasn't deleted. Did you get an error from rm? What are the permissions / ownership of that directory? And really, what else is in that directory - I wasn't asking for a vague description, give actual output. Let's kill several birds with one stone, go to that directory and run the following, paste the complete output to the forums or post it to a file sharing site and put the link here:
find -exec stat -c "%U:%G %A %N" '{}' \+Last edited by Trilby (2020-04-17 02:57:51)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
drsl:drsl drwxr-xr-x '.'
drsl:drsl -rw-r--r-- './zz.py'
drsl:drsl -rw-r--r-- './search.py'
drsl:drsl -rw-r--r-- './mysql.py'
drsl:drsl -rw-r--r-- './app.py'
drsl:drsl drwxr-xr-x './public'
drsl:drsl drwxr-xr-x './public/css'
drsl:drsl -rw-r--r-- './public/css/style.css'
drsl:drsl -rw-r--r-- './public/index.html'
drsl:drsl -rw-r--r-- './public/im1.JPG'
drsl:drsl -rw-r--r-- './public/im2.JPG'
drsl:drsl drwxr-xr-x './public/js'
drsl:drsl -rw-r--r-- './public/js/helper.js'
drsl:drsl -rw-r--r-- './public/js/resultscript.js'
drsl:drsl -rw-r--r-- './public/js/indexscript.js'
drsl:drsl drwxr-xr-x './__pycache__'
drsl:drsl -rw-r--r-- './__pycache__/mysql.cpython-38.pyc'
drsl:drsl drwxr-xr-x './tutorials'
drsl:drsl drwxr-xr-x './tutorials/public'
drsl:drsl drwxr-xr-x './tutorials/public/css'
drsl:drsl -rw-r--r-- './tutorials/public/css/viewstyle.css'
drsl:drsl -rw-r--r-- './tutorials/public/css/style.css.bkup'
drsl:drsl -rw-r--r-- './tutorials/public/css/view.html'
drsl:drsl -rw-r--r-- './tutorials/public/css/style.css'
drsl:drsl -rw-r--r-- './tutorials/public/index.html'
drsl:drsl drwxr-xr-x './tutorials/public/js'
drsl:drsl -rw-r--r-- './tutorials/public/js/script.js'
drsl:drsl -rw-r--r-- './tutorials/1.py'
drsl:drsl drwxr-xr-x './doc'
drsl:drsl -rw-r--r-- './doc/notes.log'
drsl:drsl -rw-r--r-- './doc/notes.pdf'
drsl:drsl -rw-r--r-- './doc/notes.aux'
drsl:drsl -rw-r--r-- './doc/notes.tex'rm doesn't return with error, but the file gets re-populated.
Offline
You have a mysql.py in that directory. That's the problem.
You could pop the first entry of sys.path which should prevent the import mechanism from trying to import from the current directory. But this would be fragile and may have side effects you'd not want. A much better approach is don't name your top-level python files after (non-system) modules you plan to import.
Last edited by Trilby (2020-04-17 03:29:36)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
That solved it. I renamed the mysql.py. What about the file under __pycache__? How did it get there? Should I worry about it?
Offline
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline