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… main.py mypackage/ __init__.py mymodule.py myothermodule.py …with a mymodule.py like this… #!/usr/bin/env python3 # Exported function def as_int(a): return int(a) … Read more

How do I check which version of Python is running my script?

This information is available in the sys.version string in the sys module: >>> import sys Human readable: >>> print(sys.version) # parentheses necessary in python 3. 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] For further processing, use sys.version_info or sys.hexversion: >>> sys.version_info (2, 5, 2, ‘final’, 0) # or >>> sys.hexversion 34014192 To … Read more

Relative imports for the billionth time

Script vs. Module Here’s an explanation. The short version is that there is a big difference between directly running a Python file, and importing that file from somewhere else. Just knowing what directory a file is in does not determine what package Python thinks it is in. That depends, additionally, on how you load the … Read more

What is setup.py?

setup.py is a python file, the presence of which is an indication that the module/package you are about to install has likely been packaged and distributed with Distutils, which is the standard for distributing Python Modules. This allows you to easily install Python packages. Often it’s enough to write: $ pip install . pip will … Read more

How do I check if directory exists in Python?

Use os.path.isdir for directories only: >>> import os >>> os.path.isdir(‘new_folder’) True Use os.path.exists for both files and directories: >>> import os >>> os.path.exists(os.path.join(os.getcwd(), ‘new_folder’, ‘file.txt’)) False Alternatively, you can use pathlib: >>> from pathlib import Path >>> Path(‘new_folder’).is_dir() True >>> (Path.cwd() / ‘new_folder”https://stackoverflow.com/”file.txt’).exists() False

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