What care should I take when naming a file in Python?

It is quite common to see problems generated because the file in Python was named with the same name as a library.

# requests.py

import requests

response = requests.get(...)

This snippet of code would generate the error:

AttributeError: partially initialized module' requests 'has no attribute' get '

So, what are all the precautions I should take when naming a file in Python and why?

Author: Woss, 2020-10-21

1 answers

When naming a file, i.e. a module, the programmer must pay attention to the name itself and its shape.

The module name should not have the same name as a library that will be used in your program for the fact described in the question.

Already the form of the name, should follow PEP8 which says which name of modules should use lowercase letter, be short and can use underscore (_) if it improves the reading of the name. On the other side, package names follow the same rule, but the use of underscore is discouraged.

My first two cents . Know the libraries that vc will use in your project and Never use the name the modules and packages of your project with the name of these libraries.

But, if you like to live dangerously, or do juggling, acrobatics, or if you "don't hit the cachola well" (laughs), you always have a way.

  1. Install to library requests (pip install requests)
  2. create a module (file) named requests.py with the contents below
import requests

response = requests.get("http://www.google.com)

print(responses)
  1. run the file with python requests.py

You will receive a message like the one below:

AttributeError: module 'requests' has no attribute'get'

  1. now get ready for your double twist carpado by modifying the file to.
import importlib.util
import sys

module_name = "requests"
file_path = "/caminho/para/a/biblioteca/lib/python3.7/site-packages/requests/__init__.py"

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

import requests

response = requests.get("http://www.google.com")

print(response)
  1. the answer it will be

Note : realize that you can assign any name to module_name, so if you make a triple death stretched with fifteen pirouettes you can do the below

import importlib.util
import sys

module_name = "QUALQUER"
file_path = "/caminho/para/a/biblioteca/lib/python3.7/site-packages/requests/__init__.py"

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

import QUALQUER

response = QUALQUER.get("http://www.google.com")

print(response)
  1. the answer will be

If you were bold enough to try it all, congratulations. But I leave my last two cents

IF YOU WANT TO PLAY, EVERYTHING FINE... BUT PLEASE DON'T DO THIS IN A REAL PROJECT

 0
Author: Paulo Marques, 2020-10-21 17:55:25