6,060 questions
Advice
1
vote
4
replies
93
views
mypy complains about a missing optional module
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_module
except ...
4
votes
2
answers
144
views
How does Python decide whether the last part of an imported name is a module or a function?
My first question is whether the code in main.py is valid?
This is main.py:
import foo.bar as fb
fb()
This is foo/__init__.py:
from .bar import * # DELETE THIS
bar = lambda: print('lambda bar')
foo/...
1
vote
0
answers
162
views
What is the documented behaviour (if any) when someone imports `pkg.__init__[ as pkg]` instead of `import pkg`?
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, ...
1
vote
2
answers
63
views
Python catch ImportError without catching transitive errors raised by the module
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.py
try:
...
-2
votes
1
answer
87
views
Is it possible to install submodules of a python package dynamically?
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 ...
0
votes
0
answers
97
views
Setuptools : why editable mode does not work outside of my project directory?
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 ...
0
votes
2
answers
161
views
One liner for printing python's path [duplicate]
I am trying to put in my Bash script a one-liner that would print my python's path
python -c 'import sys; for p in sys.path: print(p)'
the for keyword is flagged as an invalid syntax
I was expecting ...
1
vote
2
answers
115
views
how to import module with TYPE_CHECKING as true
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.
...
-3
votes
1
answer
82
views
Importing some symbol from a Python script into its test script
I've got the following project tree:
SomeDir
|- script.py
|- TestDir
| |- test.py
script.py is designed to be called in CLI and contains several classes: notably one that does the actual job and one ...
6
votes
1
answer
219
views
Is there a short way to write "import" and "from import" for the same module?
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 ...
2
votes
1
answer
108
views
My privately developed and installed module gives "ModuleNotFoundError: No module named 'jbpy'" at import
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 ...
1
vote
0
answers
77
views
How is import os.path possible? [duplicate]
Since os is a module instead of a package, import os.path should fail. For comparison:
>>> import os.sys
Traceback (most recent call last):
File "<python-input-0>", line 1, ...
0
votes
1
answer
106
views
Use importlib.resources for files inside the top level parent module
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 ...
1
vote
2
answers
96
views
Handling Missing Dependencies Gracefully in Python Modules
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 ...
0
votes
1
answer
140
views
Build python project with custom packages using pyinstaller
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/
│ │ ├── ...