This is because if you get a Fatal Python error and think your PyCharm environment is corrupted, it's because of the .py filename. I want to think that there are beginners.
Create a virtual environment of python 3.7 with Windows10 + Anaconda and run it with PyCharm This article was written by someone who is already afraid of python, as it has been so difficult to get to this combination.
I wrote the following code to create a .py file to check the operation of reprlib that came out while studying for the Python3 engineer exam.
python
import reprlib
reprlib.repr(set('abcdefg'))
Since it is 2 lines, if you execute it super-easily, you will get an error. .. ..
python
Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
File "C:\XXXXXXXX\Anaconda3\envs\py37\lib\site.py", line 168, in addpackage
exec(line)
#(Omitted)
File "<Project folder>\reprlib.py", line 2, in <module>
reprlib.repr(set('abcdefg'))
AttributeError: module 'reprlib' has no attribute 'repr'
I thought it wasn't because of the program because it was a program according to the example of the exam, and I expected that the environment might have changed since the last time I used PyCharm. So for the time being, I tried to check if the python program that was running until now works.
python
a = 100
b = 50
c = a + b
print(c)
It ’s just addition,
python
Fatal Python error: initsite: Failed to import the site module
#(Omitted)
File "<Project folder>\reprlib.py", line 2, in <module>
reprlib.repr(set('abcdefg'))
AttributeError: module 'reprlib' has no attribute 'repr'
I'm not using reprlib, but I get the exact same error as before. ⇒ PyCharm cannot load the program ...?
When I searched for "Failed to import the site module", I found that the python version was different, the path was wrong, and the module name was wrong. Since PyCharm does not seem to be able to read the program, check the following from Anaconda Navigator. --Virtual environment can be activated with Environments --You can type python in the terminal --No error when typing import reprlib in terminal ⇒ Anaconda seems to have no problem. After all the setting of PyCharm is wrong ...?
I don't remember changing the settings by myself, so I decided that it would be quick to try the reinstallation for the time being. Uninstall PyCharm → Reinstall and then run the addition program again.
python
Fatal Python error: initsite: Failed to import the site module
#(Omitted)
File "<Project folder>\reprlib.py", line 2, in <module>
reprlib.repr(set('abcdefg'))
AttributeError: module 'reprlib' has no attribute 'repr'
"N" "" "" ""
Is the connection between PyCharm and the virtual environment strange? I thought ~~ Yakekuso de ~~ Recreate the virtual environment with another name → Reset the interpreter from PyCharm and execute the addition program.
python
Fatal Python error: initsite: Failed to import the site module
#(Omitted)
File "<Project folder>\reprlib.py", line 2, in <module>
reprlib.repr(set('abcdefg'))
AttributeError: module 'reprlib' has no attribute 'repr'
I'm tired of seeing it.
I reinstalled PyCharm, but is there something that wasn't erased when I uninstalled it? ?? By the way, when I reinstalled, I went to read Config. I thought, I checked in the project folder of PyCharm. Then I found a suspicious folder called \ _ \ _ pycache \ _ \ _! !! This is it! !!
Deleted the \ _ \ _ pycache \ _ \ _ folder and reinstalled PyCharm for the second time today. I prayed that it would work because the environment was beautiful this time, and executed the addition program.
python
150
Process finished with exit code 0
Look! !! And run the reprlib program! !!
python
Fatal Python error: initsite: Failed to import the site module
#(Omitted)
File "<Project folder>\reprlib.py", line 2, in <module>
reprlib.repr(set('abcdefg'))
AttributeError: module 'reprlib' has no attribute 'repr'
Oh God. .. ..
I'm sure there's still something left, and when I check the project folder again, I find another \ _ \ _ pycache \ _ \ _ folder. Speaking of which, I wondered what was in it.
reprlib.cpython-37.pyc
Hmm?
The extension pyc is a file created when python is compiled. Creation timing:
Although it appears in the error message, the .py file name for import reprlib this time was reprlib.py. When this is executed, a new reprlib will be compiled under the project folder with the same name as the module to be imported, so it seems that python went to execute the new one and could not call the correct reprlib. I will. (Synonymous with making a defective self-made module) I'm not sure why the addition program that didn't import reprlib didn't work either. .. .. I'm guessing, maybe the .pyc search in the project folder is done before the .py.
I changed the name of the .py file for import reprlib from reprlib.py to another name, and it was easy to execute.
In addition, normal \ _ \ _ pycache \ _ \ _ is under ~ \ Anaconda3 \ envs \
It was poor.
Recommended Posts