[PYTHON] If you think the PyCharm environment is broken, it's because of the file name

Introduction

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.

environment

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.

Background to the error

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'

Flow until error resolution

Check the program that was running until now

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 ...?

Ask Google teacher

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 ...?

Reinstall PyCharm

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" "" "" ""

Recreating the virtual environment

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.

Suspect cache

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! !!

Reinstall PyCharm

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. .. ..

Get revelation

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?

What happened

The extension pyc is a file created when python is compiled. Creation timing:

  1. Module import first time
  2. When the pyc file already exists after the second module import, but the time stamp is older than the py file. Reference: https://techacademy.jp/magazine/19131

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.

solution

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 \ \ Lib for Anaconda. If you haven't created your own module but it's under the project folder, you should doubt it.

Lesson

  1. Make the .py file name different from the existing module name.
  2. Find out more about python before it works. (PyCharm didn't need to be reinstalled if I was looking into what pycache was)

It was poor.

Recommended Posts

If you think the PyCharm environment is broken, it's because of the file name
When you think the update of ManjaroLinux is strange
If you want a singleton in python, think of the module as a singleton
If you are told cannot by Python import, review the file name
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
Python property description is strange if you look closely (it's not strange if you think more carefully)
The strongest PySide / PyQt development environment is also PyCharm
Specify the project name of docker-compose with Docker integration of Pycharm
Think about the analysis environment (Part 1: Overview) * As of January 2017
If __name__ == Raise your hand, if you write the code under'__main__'
[Pandas speedup] If you think merge is slow, use map
What to do if you cat or tail a binary file and the terminal is garbled
If you upgrade the kernel version when the host OS is Linux, an error occurred when launching the virtual environment of VirtualBox, so a memorandum