6,060 questions
Advice
1
vote
4
replies
67
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
141
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
105
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
95
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
138
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/
│ │ ├── ...
0
votes
0
answers
30
views
How to manage internal dependencies within a project when using `pip install -e .`?
Detail of the Problem:
My simple project structure is as follows:
└─mris
├─mris.egg-info
├─seggms
│ ├─training
│ │ └─run_training.py
├─utilities
│ └─config.py (contains ...
0
votes
0
answers
87
views
Nuitka onefile can't find directory for dynamic extension loading
I'm working on a Pycord bot project, and I'm using Nuitka to compile it. My goal is to compile the main script as a single file (--onefile) for distribution, but still be able to dynamically load ...
2
votes
1
answer
80
views
Changing a variable in another file, with a function in that file
I have two files. file1 and file2.
file1:
color = (0, 0, 0)
class drawing:
def changeColor(newColor):
global color
color = (newColor)
def drawCircle(x, y, r, c=color)
#...
0
votes
0
answers
46
views
Using a folder named collections where I defined my own modules causes ModuleNotFoundError
I created a folder named collections, inside I defined some collections-related modules. When I went to used them, I got an ModuleNotFoundError.
Changing the name of collections to my_collections ...
-3
votes
2
answers
110
views
Renaming a Python module using import as [duplicate]
I'm curious as to why this doesn't work, resulting in:
ModuleNotFoundError: No module named 'maths'
import math as maths
from maths import factorial
1
vote
0
answers
228
views
Make Pylance suggest all imports for my code when working in a separate directory
With VSCode Pylance, many import suggestions are missing when working under these conditions:
Package installed in editable mode
I'm working in a separate directory
Original package does not contain ...
0
votes
0
answers
52
views
How do I change a global constant of a loaded module so that other global variables of the loaded module that depend on it reflect the change?
This question may have a simple answer. I have a near-trivial module0.py:
a = 1
b = a + 3
I import this module into a script and redefine module0's global constant a:
import module0 as m0
m0.a = 2
...
2
votes
1
answer
68
views
What's `_distutils_hack.DistutilsMetaFinder` in `sys.meta_path`?
The document of Python's import system tells that there are 3 default finders/importers (built-in, frozen, and the import path one). But when trying to get the repr of sys.meta_path, I got another ...
-2
votes
1
answer
187
views
Why is python thinking my folder is a module?
I have a project structured like this:
project+
|-folder1+
| |-__init__.py
| |-script
|
|-folder2+
| |-__init__.py
| |-...
1
vote
0
answers
76
views
Why did I need to add an empty __init__.py, and why was a later sys.path entry overriding an earlier one?
I created a test project with the root directory named test_import to reproduce the problem. When I ran the following code to print the sys.path list:
import sys
for path in sys.path:
print(path)
...
0
votes
0
answers
79
views
Why won't the C# .NET DLL import?
Here is my code:
import clr
clr.AddReference("PixelPilot.Core")
from PixelPilot.Core import *
I have already tried renaming the DLL and also trying different names to import it. (EX. ...
0
votes
0
answers
70
views
Import Helpers.py from different directory
I am making a React app.
I am using some Python code to automate a few things.
But currently I am duplicating some Python code across separate components. I would like to avoid this, and place all my ...
0
votes
1
answer
106
views
How to manually create a namesapce package import from a list of paths?
According to PEP420, namespace packages have the lowest precedence. For example, import xxx will not create a namespace package xxx if there are any xxx/__init__.pys or xxx.{py,pyc,so,pyd}s in any ...
-2
votes
1
answer
129
views
How to make script-level variables private to the script [duplicate]
Is it possible to declare a global variable that is invisible to the importing script?
For example, in script a.py I have a variable var_a that is accessible to any function in a.py. However, in ...
-4
votes
2
answers
143
views
How to filter variables for import in Python
I really need to solve my problem because one of the scripts I built, there are hundreds of variables; and in the other script, I don't want to import all variables but only specific variables using ...
0
votes
0
answers
33
views
Correct way to import Python classes with Git submodules?
I have a git repo, core, which I would like to use as a submodule within other git repos, e.g. example. core contains python files within which are classes that I would like to import into example. I ...
0
votes
1
answer
108
views
Importing one Jupyter notebook file from another so that changes from the imported file can be accessed without restarting the kernel
I have two Jupyter notebook files named Test1.ipynb and Test2.ipynb. I would like to be able to import Test2.ipynb from Test1.ipynb in such a way that any changes to Test2.ipynb will take effect ...
0
votes
3
answers
165
views
Reason for no relative imports in python scripts
I understand that in Python, relative imports are only allowed inside of packages. Personally, I find this functionality great; it makes it very easy to import stuff from other modules.
However, for ...
1
vote
1
answer
111
views
Using Alias in __init__.py Still Exposes Original Submodule Name in Package Namespace in Python
I noticed unexpected behavior when importing a submodule with an alias in my package's __init__.py file. In short, using an alias in __init__.py still exposes the original submodule name in the ...
0
votes
1
answer
97
views
No instabot in Python even though I installed it via the command prompt
I want to incorporate an instabot in my code.
I downloaded Instabot and installed it through the Windows' command prompt pip install -U instabot as Spyder didn't recognize my user: C:\Users\(My Name) ...
-1
votes
1
answer
79
views
Understanding import in python (initalization of sys.modules)
I recently came to know that modules like os are imported way before a user python program starts running and therfore we cannot import a custom file named os even though it's not in the sys....
1
vote
1
answer
116
views
Undertanding python import process (importing custom os module)
I was reading through the python docs for how import is resolved and found this
... the interpreter first searches
for a built-in module with that name. These module names are listed in
sys....
2
votes
2
answers
83
views
How to prevent imported Python module from being included in .exe
I am trying to create an executable from a Python file using auto-py-to-exe. My code is set up as two .py files. I have a top level code which I will call main.py, and a secondary code with my ...
0
votes
0
answers
56
views
Importing python files from other directories?
I'm writing a resource mining game at the moment, and I want players to be able to easily add "expansions" to the game which add on more resources or mechanics. I have a structure that looks ...
2
votes
2
answers
87
views
Test that module is NOT imported
I need to test that specific imports of my library don't import along some other heavy modules (e.g. tensorflow - we'll use it in this question). Here's a detailed description:
The test should run ...
1
vote
2
answers
117
views
Python adaptor for working around relative imports in my QGIS plugin?
I am writing a QGIS plugin. During early development, I wrote and tested the Qt GUI application independently of QGIS. I made use of absolute imports, and everything worked fine.
Then, I had to adapt ...
0
votes
0
answers
42
views
Python unittest failing on function importing local file
I have a Python package with a directory that looks like this:
-root
|
|-src
| |
| -app
| |
| |-__init__.py
| |-__main__.py
| |-file1.py
| |-file2.py
|
|-tests
|
|-__init__....
-1
votes
1
answer
82
views
Why won't this Python module import into my code [duplicate]
I have this entry point:
# main.py
from 022 import *
print("Hello from main.py")
I have 022.py also. It is:
print("Hello World")
When I run main.py I get this error:
File &...
-3
votes
1
answer
109
views
import pytwain ModuleNotFoundError: No module named 'pytwain' [closed]
Here is my error: although I installed pytwain and it is located in C:\Users\User\Documents\systhen\myenv310\Lib, I tried working with several Python versions (3.13, 3.12, 3.10, 3.9), but the same ...
0
votes
1
answer
195
views
Best practices for Python imports in a production project: handling relative/absolute imports across different directories and test cases
I have the following folder structure in python:
my_project/
├── Dockerfile
├── Makefile
├── run.py
├── data/
│ ├── raw/
│ └── processed/
└── src/
├── __init__.py
├── config.py
├── ...
1
vote
1
answer
62
views
How do I import from a package in a subdirectory?
I have David Eppstein's PADS library of Python Algorithms and Data Structures in a subdirectory next to my Python script:
- my-script.py
- PADS
- __init__.py
- Automata.py
- Util.py
- ....
0
votes
1
answer
40
views
Python Import Error: Confusion between package and module with the same name
I have a Python project with the following structure:
├── main.py
├── pack1
│ ├── __init__.py
│ ├── mod1
│ │ ├── __init__.py
│ │ └── request.py
│ ├── mod1.py
│ └── mod2.py
The content ...
5
votes
1
answer
6k
views
How to properly deal with [import-untyped] in mypy?
I entered the command:
$ mypy mypycode.py
Then I run the following code in mypycode.py:
from mypyc.build import mypycify
from setuptools import setup
from timeit import timeit
import math
setup(
...
4
votes
0
answers
38
views
ImportError: libgridstore.so.0 when importing GridDB in Python3.10
I'm trying to get GridDB working on my WSL/Ubuntu environment. I have followed https://pypi.org/project/griddb-python/ to install griddb and was successful but when trying to import the module I'm ...
17
votes
4
answers
47k
views
ModuleNotFoundError while importing moviepy.editor
I'm trying to work with VideoFileClip and vfx functions from the Moviepy library but my interpreter keeps throwing a 'ModuleNotFoundError: No module named 'moviepy.editor''.
I've installed and ...