Difference between Module and Class in Python

There are huge differences between classes and modules in Python. Classes are blueprints that allow you to create instances with attributes and bound functionality. Classes support inheritance, metaclasses, and descriptors. Modules can’t do any of this, modules are essentially singleton instances of an internal module class, and all their globals are attributes on the module … Read more

How to import a module from a different folder?

Firstly, this import statement: from models import some_model should be namespaced: # in myproject/backend/backend.py or myproject/api/api.py from myproject.models import some_model Then you will need to get the directory which contains myproject, let’s call this /path/to/parent, into the sys.path list. You can do this temporarily by setting an environment variable: export PYTHONPATH=/path/to/parent Or, preferably, you can … Read more

Error while finding spec for ‘fibo.py’ (: ‘module’ object has no attribute ‘__path__’)

There are two ways you can run a Python 3 script. python fibo.py: The argument is the name of the .py file. Dots are part of the filename. python -m fibo: The argument is the name of a Python module, without .py. Dots indicate packages; fibo.py means “the module py in the package fibo.” This … Read more

Python: ‘Private’ module in a package

I prefix private modules with an underscore to communicate the intent to the user. In your case, this would be mypack._mod_b This is in the same spirit (but not completely analogous to) the PEP8 recommendation to name C-extension modules with a leading underscore when it’s wrapped by a Python module; i.e., _socket and socket.

How does python find a module file if the import statement only contains the filename?

http://docs.python.org/3/tutorial/modules.html#the-module-search-path 6.1.2. The Module Search Path When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations: The directory containing the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)