Relative imports in Python 3

unfortunately, this module needs to be inside the package, and it also needs to be runnable as a script, sometimes. Any idea how I could achieve that? It’s quite common to have a layout like this… …with a mymodule.py like this… …a myothermodule.py like this… …and a main.py like this… …which works fine when you run main.py or mypackage/mymodule.py, but fails with mypackage/myothermodule.py, due to … Read more

Python Variable Declaration

Okay, first things first. There is no such thing as “variable declaration” or “variable initialization” in Python. There is simply what we call “assignment”, but should probably just call “naming”. Assignment means “this name on the left-hand side now refers to the result of evaluating the right-hand side, regardless of what it referred to before … Read more

How do I upgrade the Python installation in Windows 10?

Every minor version of Python, that is any 3.x and 2.x version, will install side-by-side with other versions on your computer. Only patch versions will upgrade existing installations. So if you want to keep your installed Python 2.7 around, then just let it and install a new version using the installer. If you want to … Read more

Python: ‘ModuleNotFoundError’ when trying to import module from imported package

FIRST, if you want to be able to access man1.py from man1test.py AND manModules.py from man1.py, you need to properly setup your files as packages and modules. Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. … When importing the package, Python searches through the directories on sys.path looking … Read more

Relative imports – ModuleNotFoundError: No module named x

TL;DR: You can’t do relative imports from the file you execute since __main__ module is not a part of a package. Absolute imports – import something available on sys.path Relative imports – import something relative to the current module, must be a part of a package If you’re running both variants in exactly the same way, one of them should work. Here is … Read more