I have a module (let's name it optional_module) that I want to be imported optionally, as it can be either present or absent. Now I do it this simple way:try: import optional_moduleexcept ...
My first question is whether the code in main.py is valid?This is main.py:import foo.bar as fbfb()This is foo/__init__.py:from .bar import * # DELETE THISbar = lambda: print('lambda bar')foo/...
To be clear, I'm not suggesting anyone actually should import pkg.__init__ directly. This is to understand potential pitfalls if someone decides to convert a module-only distribution into a package, ...
In Python, when an import fails, how can I differentiate between:The module doesn't exist.The module exists, but it tried importing another module that didn't exist.Example# ./first.pytry: ...
I have a very complex Python library that is used by several people/projects for different purposes.The structure is basically the same as many Python libraries, but I would like to give the ability ...
I don't manage to make the editable mode pip install -e . for a local installation of my project. After the installation, when I import a constructor from a module of my package within in python shell ...
I am trying to put in my Bash script a one-liner that would print my python's pathpython -c 'import sys; for p in sys.path: print(p)'the for keyword is flagged as an invalid syntaxI was expecting ...
I want to generate a schema based on a class defined in another file, so i need to know the return type of these functions. However, the annotation might be based on some imports under TYPE_CHECKING....
I've got the following project tree:SomeDir|- script.py|- TestDir| |- test.pyscript.py is designed to be called in CLI and contains several classes: notably one that does the actual job and one ...
I find myself mixing both import x and from x import y forms for the same module, depending how often a particular object is used and whether it is clear which modules it comes from.For example I ...
I'm implementing my own Python module package, called jbpy. I'm using setuptools with a pyproject.toml file as the build system.I'm working on Ubuntu 24.04, but I also get the error under WSL on a ...
Since os is a module instead of a package, import os.path should fail. For comparison:>>> import os.sysTraceback (most recent call last): File "<python-input-0>", line 1, ...
I want to load a file from my python module using importlib.resources (see also this question).If the file is inside a submodule, this is straight forward (run using python -m, see here):import ...
I'm currently working on a modular python framework. The current dilemma is, that one module has a set of submodules that have vastly different dependencies, and a user usually would only use one of ...
How to build a python project into a single executable if it has files split in multiple directories?This is my project structure:├── main.py├── main.spec├── src/│ ├── GUI/│ │ ├── ...