You are not logged in.

#1 2020-04-16 20:52:30

doubleslash
Member
Registered: 2011-08-16
Posts: 80

[SOLVED]Cannot import python module

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

python

and then inside the shell,

import mysql.connector

returns 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

#2 2020-04-16 20:57:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,516
Website

Re: [SOLVED]Cannot import python module

What's in the directory where it fails?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2020-04-17 02:48:06

doubleslash
Member
Registered: 2011-08-16
Posts: 80

Re: [SOLVED]Cannot import python module

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

#4 2020-04-17 02:57:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,516
Website

Re: [SOLVED]Cannot import python module

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

#5 2020-04-17 03:08:52

doubleslash
Member
Registered: 2011-08-16
Posts: 80

Re: [SOLVED]Cannot import python module

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

#6 2020-04-17 03:18:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,516
Website

Re: [SOLVED]Cannot import python module

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

#7 2020-04-17 04:27:35

doubleslash
Member
Registered: 2011-08-16
Posts: 80

Re: [SOLVED]Cannot import python module

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

#8 2020-04-17 12:39:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,516
Website

Re: [SOLVED]Cannot import python module


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB