PyCharm 2022.1 and Python 3.10.
When I try to run a Pytest script, I see the following error:
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File "z:\Users\...\AppData\Local\Programs\Python\Python310\lib\site.py", line 617, in <module>
main()
File "z:\Users\...\AppData\Local\Programs\Python\Python310\lib\site.py", line 594, in main
known_paths = removeduppaths()
File "z:\Users\...\AppData\Local\Programs\Python\Python310\lib\site.py", line 139, in removeduppaths
dir, dircase = makepath(dir)
File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\site.py", line 97, in makepath
dir = os.path.join(*paths)
AttributeError: module 'os' has no attribute 'path'
Why could that be happening?
If you import a build-in or standard library module, and it seems to lack some attribute it shall normally have like in this case, the first thing to check is for another module with the same name that is in the Python path and is used instead of the standard module (which was the case here).
This can be checked in the debugger or by printing the module in question (it shall show the path it is imported from).
If the module is the correct one, there may be a problem with the installation of the module, or with Python itself, an example was mentioned in the comments by bad_coder. It could also be an effect of a mixup of different virtual environments. In both cases, a clean reinstall may fix the problem.