0

I feel a bit confused with how python modules work when I started looking at PyMySQL repository, see here: https://github.com/PyMySQL/PyMySQL?files=1

1) Why is there no pymysql.py file because it is imported like: import pymysql? Isnt it required to have such a file?

2) I cannot find the method connect, used like: pymysql.connect(...), anywhere. Is it possible to rename exported methods somehow?

Ludvig W
  • 744
  • 8
  • 27
  • if you want to use the module just use ` pip install PyMySQL` in your project directory and it will be alright. – Arya11 Jan 31 '20 at 18:02
  • A file is not a module. Executing the code in `foo.py` is *one* way to create a module named `foo`. Another way is to execute the code in `foo/__init__.py`. – chepner Jan 31 '20 at 18:21

1 Answers1

1

There's a directory pymysql there. A directory can also be imported as a module*, with the advantage that it can contain submodules. Classically, there's a __init__.py file in the directory that controls what's in the top-level pymysql.* namespace.

So, the connect method you're missing will either be defined directly in pymysql/__init__.py, or defined in one of its siblings in that directory, and then imported from there by pymysql/__init__.py.

*Strictly speaking, a directory that you import like a module is really called a "package". I like to avoid that term—it's potentially confusing because the term is overloaded: what you install with pip is also called a "package" in sense 2, and that might actually contain multiple "packages" in sense 1.

See What is __init__.py for? and the official docs

jez
  • 14,867
  • 5
  • 37
  • 64