417 questions
2
votes
1
answer
65
views
Dynamically Loading Modules of the Same Name but Different Paths
I'm making a sort-of mod loader that automatically loads certain classes with a specific variable defined from any files in the root directories' mods folder.
My file structure is as follows:
mods
- ...
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 ...
0
votes
1
answer
63
views
Why does inspect fail to get source file for classes in a dynamically-imported module?
inspect.getsource() and inspect.getsourcefile() can access source info for a function, but not for a class, when they are in a module that is imported dynamically with importlib.
Here are two files, ...
1
vote
0
answers
90
views
importlib resources files -> not a package, empty module
I tend to alwasy run the testers from inside the module folder, but this breaks the "resources.files" functionality, as it seems not to be able to find the module any more
module folder
...
0
votes
0
answers
100
views
How to properly import python module from another python file using importlib.import_module?
I'm working on Alembic migration github action and need to import base metadata class inside python migration script which locates in another directory. I can't import it directly inside python ...
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 ...
0
votes
1
answer
91
views
Check if a python module exists with specific venv path
If I have a venv path, how can I find out if a Python module is installed inside that venv?
I normally use importlib.util.find_spec. But this only works for the current venv that is active and does ...
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 ...
0
votes
1
answer
65
views
Why does importlib.resources read_text raise UnicodeDecodeError on Windows but not mac and RPi?
I am writing a small web app (https://github.com/r-owen/base_loom_server) and it runs on macOS and Raspberry Pi, but a Windows user reports an exception when Python reads a UTF-8-encoded resource file ...
0
votes
1
answer
52
views
python importlib from subdirectory, module not found
I'v been struggling with importing a sub module. In main.py two modules are imported located under 220_04_070. I'm using importlib since the directory name only consist of numbers. common.py just ...
0
votes
2
answers
176
views
How to dynamically import functions from a file in a subdirectory? [duplicate]
I am trying to dynamically import functions from a file in a subdirectory.
For example:
workspace
--|main.py
----|subfolder1
------|subfolder2
--------|file1.py
--------|file2.py
The file main.py ...
1
vote
0
answers
52
views
Overriding pip package
I am trying to setup Octoprint on a new orangepi 3b board. It has been a bit problematic.
One problem is about access to gpio for all the existing plugins. The standard RPi.GPIO python library does ...
5
votes
2
answers
712
views
Django autoreload raises: TypeError: unhashable type: 'types.SimpleNamespace'
When I upgrade importlib_meta from version 8.4.0 to 8.5.0 (released just yesterday, Sep 11 2024), I get the following error when I start running the development server with python manage.py runserver:
...
1
vote
0
answers
1k
views
Reloading torch triggers RuntimeError
When trying to reload the torch library in Python, by running the following lines of code, I encountered the error below:
import torch
import importlib
importlib.reload(torch)
------------------------...
0
votes
1
answer
89
views
OSError: Bad file descriptor
I'm trying to run celery beat worker and a normal celery worker as docker containers. I have my tasks in flask application.
The normal celery worker is working fine but getting following error in beat ...
0
votes
1
answer
64
views
How do I dynamically define a package level __all__ without running any slow code in the package's constituent modules?
TL;DR: How do I read a module's __all__ definition and dynamically add it to the package-level __init__.py without actually running any slow code in the module itself?
I am writing a library and have ...
-1
votes
1
answer
68
views
Reload custom python function
I'm trying to reload from a python script a custom function because i'm editing the function constantly and i dont want to reload the kernel each time i make a change on the file. I have read that i ...
0
votes
2
answers
78
views
Why undeclared variables are present in `globals()` after a reload and is it safe to use them to identify a reload? [duplicate]
I've found that the snippet below when reloading a module test unexpectedly has all variables already defined in globals()/locals().
Why this happens?
I've noticed this "xxx" in locals() ...
1
vote
0
answers
116
views
Is there a way to get the python package distribution (wheel) name for a given module?
Is there a way to get the python package distribution (wheel) name for a given module (python object given by import ...)?
I know that it's possible to get meta-information about installed packages ...
0
votes
1
answer
52
views
How do I carry over an import carried out in a 2nd file back to the calling file in python?
The code below is a utility I wrote that installs and restarts a script automatically without having the user need to parse through errors to install. Ideal usage would be below:
#file1.py
from file2 ...
0
votes
2
answers
169
views
Import a script from a parent's subdir, with a filename and dirname starting with digits
I know the usual Python import system, packages, __init__.py, the __import__ function (see In python, how to import filename starts with a number), etc. I know the classic How do I import other Python ...
0
votes
0
answers
30
views
Multiple tests for output of a module reading standard input
In one coding exercise in my Udemy course, a student is required to write a program that checks whether the total length of three strings read from the standard input is equal to 10, i.e. a one-liner ...
1
vote
1
answer
191
views
How to Override a Package's __init__.py Without Breaking Submodule Imports in Python?
I'm working on a project where I need to override files located in the ./lib/python3.9/site-packages" directory. I have the following hook that does just that using importlib.util and sys....
0
votes
1
answer
817
views
How to get the distribution name of an imported package in Python3.10+
I have multiple distributions that each provide a Python package of the same name (because they have the same API and same functionality, just different implementations). I want to be able to report ...
0
votes
0
answers
301
views
Installing geopandas in a QGIS Plugin?
I'm trying to install geopandas for use in a QGIS Plugin.
create a venv
install geopandas
add site-packages to path
import geopandas
# sys.executable - path to qgis python interpreter
# venv_path - ...
1
vote
0
answers
604
views
Access files within python package with python 3.12 and importlib
I'm trying to figure out what the best practice is when it comes to reading data from within a package.
I understood that in python 3.12 I should use importlib.resources, see e.g. How to read a (...
0
votes
0
answers
1k
views
Module 'importlib.resources' has no attribute 'files'
I'm working on some graphs using NetworkX using Spyder and I've encountered this error involving the importlib.resources.
Here's my code.
import networkx as nx
from importlib import files
# Create an ...
0
votes
1
answer
890
views
Python 3.7 equivalent of `importlib.resources.files`
I have an application that unfortunately does not support Python past 3.7. I have some code I'm trying to port (from Python 3.12) back to Python 3.7 that uses importlib.resources.files to get the path ...
0
votes
0
answers
49
views
How to specify path in import_module function to dynamically import modules?
I have a class that should dynamically import a module depending on a parameter passed to the class's constructor. I'm doing this by using a dictionary where the keys are the possible parameter values,...
-2
votes
1
answer
73
views
ModuleNotFoundError when using importlib inside of a class
I am attempting to import an object using importlib.import_module, but am getting a ModuleNotFoundError no matter what I try. I've read through as many related posts as I can find, but haven't been ...
1
vote
0
answers
110
views
How can a python unit test work with data files specified in setup.py package_data
I have json files which I would like to embed within my Python package.
I've read about using package_data in my setup.py and then using importlib.resources to access the data files at runtime (as ...
0
votes
1
answer
130
views
I need to find out how to add images to the pip installer library that I use for the background and icons of the program
In order to use the image, I use the following commands
bg = Photo Image(file = "DNA.png")
label1 = Label(window, image = bg)
label1.place(x = 0, y = 0)
window.iconbitmap('DNAChain.ico')
...
0
votes
1
answer
92
views
`importlib.import_module` not finding module in working directory
I would like to import from the current working directory.
For example:
import os
import pathlib
import tempfile
from importlib import import_module
with tempfile.TemporaryDirectory() as temp_dir:
...
0
votes
2
answers
191
views
Python imported functions are not being executed - missing libraries
I am finalizing a project where I need to split the requirements.txt , helper_functions.py and , libraries.py -libraries that are used within the functions- into (3) separate files for code ...
2
votes
1
answer
506
views
Unable to start MLflow UI
Problem: Following a quicksart guide in official docs (https://mlflow.org/docs/latest/tracking/autolog.html), every time I try running mlflow ui --port 8080 inside parent directory with particular ...
0
votes
1
answer
34
views
Imports in a file imported by location generate ModuleNotFoundError
(old title: Resolve ModuleNotFoundError on import from module imported by file name)
I have the following project structure
. Root
|--lambdas/
| |-- lambda-1/
| |-- lambda_function.py
| |...
0
votes
0
answers
72
views
What does importlib.reload change for object (Enum) comparisons?
I have stumbled across a weird problem when reloading modules for which I have found a solution on the internet. However, I am missing an explanation of why this problem occurs (especially regarding ...
1
vote
1
answer
633
views
How do I access the version number of the package I'm writing?
I'm trying to build a Python project. I use Poetry to manage dependencies. In my pyproject.toml, I have the following:
[tool.poetry]
name = "gitdida"
version = "0.2.0"
My ...
3
votes
1
answer
93
views
Python search for modules on sys.path only, ignoring already loaded modules, for existence check purposes
I am looking for a solution to the following problem. I have a package that includes some build tools (for use with SCons, though that is just incidental to the question). I want the package to be ...
0
votes
0
answers
212
views
Python importlib.resources.files() throws UnicodeDecodeError: invalid continuation byte
I am creating a Python package that needs certain data files in order to work. I've been looking for a way to include these data files with the package installation. I found a way using importlib....
2
votes
0
answers
149
views
importlib-metadata package not found for PIL (Pillow)
The little program below is almost a copy-paste for an example form Astropy website. The program works fine under PyCharm environment but when trying to make it executable using PyInstaller, I'm ...
0
votes
0
answers
59
views
how to loop through .py files using importlib
I am trying to use the prefix modl and iterate through .py files that I use as settings. I'd like to stick to python files because it is easy to declare functions and variables and import them. The ...
1
vote
2
answers
1k
views
Lazy import from a module (a.k.a. lazy evaluation of variable)
Lazy imports in Python have been long discussed and some proposals (for example the PEP609 - Lazy Imports) have been made to make it a built-in (optional) feature in the future.
I am developing a CLI ...
1
vote
3
answers
3k
views
ImportError: No module named importlib
I'm trying to install scrapy on python 2.7. The problem is that when it's time to compile it I get this error
$ sudo python2 -m pip install scrapy
DEPRECATION: Python 2.7 reached the end of its life ...
1
vote
1
answer
138
views
How to get all the metadata that have the same name from a python package?
I want to read metadata from a python package to feed a table withsome informations. The package is build using setuptools so all urls are saved under the [project.urls] field in the pyproject.toml ...
0
votes
0
answers
772
views
Getting Module Not found error: importlib.resources in windows 7 python 3.7version
Getting Module Not found Error:No Module named 'importlib.resources'
Module not found error in windows 7 python version 3.7
I'm trying to run one picture in terminal using python programming after I ...
0
votes
1
answer
4k
views
Using importlib
I'm having a hard time understanding the python importlib library.
I've seen 2 implementations to dynamically import modules:
1.
spec = importlib.util.spec_from_file_location(name, path)
module = ...
0
votes
0
answers
134
views
Can I use "from importlib import abc" on python2.7
I can use from importlib import abc and from importlib import util normally on Python 3. However, when I have to run the same code on Python 2.7, I got ImportError.
I found that importlib2 seems to be ...
1
vote
1
answer
3k
views
Replacing `imp.load_source` in Python 3.12
I am trying to build Electron (basically Chromium) on Fedora Rawhide. The distro (as of 2023) ships with Python 3.12. One of the scripts ran during build fails due to a missing imp module. The ...
0
votes
1
answer
26
views
Python packaging - how to make sub-modules importable from higher-level module?
The following works in my python package:
import Top.Lower
However, the following does not work:
import Top
print(Top.Lower)
I'm using packages = find: in my setup.cfg as well as:
[options....