Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

visual studio code - ImportErrors and ModuleNotFoundError for python

It seems like my system cannot access modules from other folders. For example, if i set my directory to

~/Downloads/principles_of_programming/object-oriented-programming/fibonacci 

and run

pytest tests/test_fibonacci.py

I get

==================================================== test session starts ===================================================== 
platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersRyanDownloadsprinciples_of_programmingobject-oriented-programming, configfile: setup.cfg
collected 0 items                                                                                                              

=================================================== no tests ran in 0.05s ==================================================== 
ERROR: file or directory not found: tests/test_fibonacci.py

If I change my directory to

~/Downloads/principles_of_programming/object-oriented-programming

and run the test as above I get

==================================================== test session starts ===================================================== 
platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: C:UsersRyanDownloadsprinciples_of_programmingobject-oriented-programming, configfile: setup.cfg
collected 0 items / 1 error                                                                                                    

=========================================================== ERRORS =========================================================== 
__________________________________________ ERROR collecting tests/test_fibonacci.py __________________________________________ 
ImportError while importing test module 'C:UsersRyanDownloadsprinciples_of_programmingobject-oriented-programmingestsest_fibonacci.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
......AppDataLocalProgramsPythonPython39libimportlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
testsest_fibonacci.py:1: in <module>
    from fibonacci import fib
E   ModuleNotFoundError: No module named 'fibonacci'
================================================== short test summary info =================================================== 
ERROR tests/test_fibonacci.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
====================================================== 1 error in 0.45s ====================================================== 

Error Output

question from:https://stackoverflow.com/questions/66057257/importerrors-and-modulenotfounderror-for-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Dealing with "import" clauses, Python will first check if it is in its installation folder or whether it is installed package, if it fails, then it searches current working directory, with the relative path considered.

For example import tests.test_fibonacci means there is a folder called tests in your current working directory, and the script test_fibonacci.py is in it and python will run it from the first line to the last line in the namespace "tests.test_fibonacci".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...