Quelle est la bonne façon de corriger cette erreur ImportError?Comment réparer une erreur "ImportError: No module named ..." en Python?
je la structure de répertoire suivant:
/home/bodacydo
/home/bodacydo/work
/home/bodacydo/work/project
/home/bodacydo/work/project/programs
/home/bodacydo/work/project/foo
Et je suis dans le répertoire
/home/bodacydo/work/project
Maintenant, si je tape
python ./programs/my_python_program.py
Je reçois instantanément
ImportError: No module named foo.tasks
Le ./programs/my_python_program.py
contient la ligne suivante:
from foo.tasks import my_function
Je ne peux pas comprendre pourquoi python ne trouvera pas ./foo/tasks.py
- il est là.
Si je le fais à partir du shell Python, il fonctionne:
python
>>> from foo.tasks import my_function
Il fonctionne ne ne pas si je l'appelle via le script python ./programs/my_python_program.py
.
Alors, quel est le point d'avoir __init__.py dans le dossier racine? Il semble ne servir à rien si vous éditez sys.path ou le chemin python. –
@Editor: '__init __. Py' indique seulement que le répertoire doit être traité comme un paquet, quand son parent est dans' sys.path' ou est lui-même un paquet. –
En outre, vérifiez les autorisations sur le dossier. C'était le problème dans mon cas. – SidJ