[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.

I just wanted to check the operation of the library, but I was addicted to it.

For example, suppose you create a file called json.py to try out the json module.

json.py


import json

obj = {
    "id" : 1,
    "name" : "hoge",
}

str = json.dumps(obj)
print(str)

The code doesn't seem to be a problem ...

This will result in an error when run.

$ python3 json.py
...
AttributeError: module 'json' has no attribute 'dumps'

It is said that the json module does not have dumps.

This is because the file name itself is json, so it was loaded in preference to the standard module.

Therefore, renaming it will solve the problem.

$ mv json.py json_test.py
$ python3 json_test.py
{"id": 1, "name": "hoge"}

By the way, even if you don't have the file name json, you can get the same result if json.py exists in the same directory hierarchy.

You can also get the same result if you have a directory with the same name that contains __init__.py.

$ mkdir ./json
$ touch ./json/__init__.py
$ python3 json_test.py
Traceback (most recent call last):
  File "json_test.py", line 8, in <module>
    str = json.dumps(obj)
AttributeError: module 'json' has no attribute 'dumps'

Recommended Posts

[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
[Python] You can save an object to a file by using the pickle module.
If you want a singleton in python, think of the module as a singleton
The story of making a tool to load an image with Python ⇒ save it as another name
What to do if you get an error when installing python with pyenv
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if you run python in IntelliJ and end with an error
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
[Python] If you suddenly want to create an inquiry form
[python] Change the image file name to a serial number
Probably the easiest way to create a pdf with Python3
Solution if the module is installed in Python but you get an error in Jupyter notebook
[Python Kivy] How to create an exe file with pyinstaller
Create an audio file with the text-to-speech function with Google Text To Speak and check the text as a guide for the speech for 3 minutes.
Try to decipher the garbled attachment file name with Python
When accessing a URL containing Japanese (Japanese URL) with python3, it will be encoded in html without permission and an error will occur, so make a note of the workaround.
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
If you remove the list to be looped, you will get terrible.
Create a shell script to run the python file multiple times
How to create a heatmap with an arbitrary domain in Python
Python vba to create a date string for creating a file name
Get the formula in an excel file as a string in Python
Create a compatibility judgment program with the random module of python.
Create an Excel file with Python3
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
What to do if you couldn't send an email to Yahoo with Python.
If you want to include awsebcli with CircleCI, specify the python version
If you are told cannot by Python import, review the file name
I get a Python No module named'encodings' error with the aws command
Create a record with attachments in KINTONE using the Python requests module
If you get a no attribute error in boto3, check the version
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
Quickly create an excel file with Python #python
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
[Python] Wouldn't it be the best and highest if you could grasp the characteristics of a company with nlplot?
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"
What to do if an error occurs when loading a python project created with poetry into VS Code
Workaround if you get an error when trying to install PySide with pip
If you add sudo on ubuntu, it will be called the default python.
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
Zip-compress any file with the [shell] command to create a file and delete the original file.
Save an array of numpy to a wav file using the wave module
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
Install the python library when you're not a sudoer (or if you don't want new users to be Sudoer, but you don't mind having the Python Library installed as you like)
If an exception occurs in the function, it will be transmitted to the caller 2
[Python] I tried to get the type name as a string from the type function
If you get a long error when tabbing an interactive shell with Anaconda
If an exception occurs in the function, it will be transmitted to the caller 1
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
How to get a list of files in the same directory with python
Python tricks: a combination of enumerate () and zip (), checking if a string can be converted to a number, sorting the string as a number
Error when installing a module with Python pip
How to read a CSV file with Python 2/3
How to create a JSON file in Python
If you want to create a Word Cloud.
Steps to create a Twitter bot with python
Save the object to a file with pickle