The file name was bad in Python and I was addicted to import

I tried a little test and got hooked on giving it the same name as the module I wanted to import. It may be common sense, but I didn't know it, so I'll make a note of it.

error

Suppose you want to test the math module and write a file called math.py.

math.py


import math
print(math.pi)

Doing this will result in an error.

Execution result


$ python math.py
Traceback (most recent call last):
  File "math.py", line 1, in <module>
    import math
  File "/home/xxxx/math.py", line 2, in <module>
    print(math.pi)
AttributeError: partially initialized module 'math' has no attribute 'pi' (most likely due to a circular import)

Circular reference to yourself

The cause is that I imported myself and circulated as stated in the error.

(most likely due to a circular import)

I didn't know it right away, so I tried printing it appropriately.

math.py


print("math")
import math
print(math.pi)

Execution result


$ python math.py
math
math
Traceback (most recent call last):
  File "math.py", line 2, in <module>
    import math
  File "/home/xxxx/math.py", line 3, in <module>
    print(math.pi)
AttributeError: partially initialized module 'math' has no attribute 'pi' (most likely due to a circular import)

math is displayed twice. At last, I knew that I was referring to myself.

The second import of the same file will be ignored, so go ahead and get an error because math.pi is missing.

Conclusion

It works if you change the file name. Avoid the same file name that you want to import.

Recommended Posts

The file name was bad in Python and I was addicted to import
I want to get the file name, line number, and function name in Python 3.4
I was addicted to confusing class variables and instance variables in Python
I was addicted to scraping with Selenium (+ Python) in 2020
I want to replace the variables in the python template file and mass-produce it in another file.
What I was addicted to Python autorun
What I was addicted to with json.dumps in Python base64 encoding
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
I was able to recurse in Python: lambda
I made a program to check the size of a file in Python
I want to display the progress in Python!
Numpy's intellisense (input completion) is incomplete in VS Code and I was lightly addicted to the solution
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
Three things I was addicted to when using Python and MySQL with Docker
Python Memorandum: Refer to the text and edit the file name while copying the target file
I want to see the file name from DataLoader
I tried to graph the packages installed in Python
How to get the variable name itself in python
I tried to touch the CSV file with Python
I want to randomly sample a file in Python
I want to write in Python! (3) Utilize the mock
I was able to repeat it in Python: lambda
I was addicted to trying logging.getLogger in Flask 1.1.x
I want to use the R dataset in python
What I was addicted to when using Python tornado
Download the file in Python
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
Note that I was addicted to npm script not passing in the verification environment
What I was addicted to when combining class inheritance and Joint Table Inheritance in SQLAlchemy
[python] Change the image file name to a serial number
Get the MIME type in Python and determine the file format
How to import a file anywhere you like in Python
I tried to illustrate the time and time in C language
[Note] Import of a file in the parent directory in Python
I tried programming the chi-square test in Python and Java.
I tried to implement the mail sending function in Python
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
I tried to divide the file into folders with Python
Try to decipher the garbled attachment file name with Python
[Fixed] I was addicted to alphanumeric judgment of Python strings
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
In the python command python points to python3.8
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
[Python] How to import the library
I wrote the queue in Python
I also tried to imitate the function monad and State monad with a generator in Python
[Python] When you want to import and use your own package in the upper directory
Get the host name in Python
The story I was addicted to when I specified nil as a function argument in Go
I was addicted to multiprocessing + psycopg2
I wrote the stack in Python
Determine the date and time format in Python and convert to Unixtime
The record I was addicted to when putting MeCab on Heroku
Search the file name including the specified word and extension in the directory
[Introduction to Python] I compared the naming conventions of C # and Python.
I wrote the code to write the code of Brainf * ck in python
I want to make the second line the column name in pandas
Format the Git log and get the committed file name in csv format
How to display bytes in the same way in Java and Python