Skip to main content
Filter by
Sorted by
Tagged with
6 votes
1 answer
403 views

This is more an effort to understand how Python (3.9 in this instance) works than an effort to solve an actual problem so please bear with me and disregard the nonsensical way of m3. I just wanted to ...
2 votes
1 answer
65 views

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 - ...
11 votes
3 answers
3k views

I have MyEnum, an enumeration derived from enum, defined in a myenum.py file such as: # myenum.py import enum class MyEnum(enum.Enum): ONE = 1 TWO = 2 Then, I import this file using the ...
5 votes
3 answers
424 views

I want to update a library so that a requirement is optional (using extras_require). Unfortunately, imported classes from the optional requirement are used everywhere in the lib as type hints. Here is ...
51 votes
2 answers
41k views

I have class called 'my_class' placed in 'my_module'. And I need to import this class. I tried to do it like this: import importlib result = importlib.import_module('my_module.my_class') but it says: ...
17 votes
1 answer
2k views

My question is similar to How to import a module given the full path? however I'm not importing a .py source file, I'm importing a package with a .pyd. During runtime I am creating new package ...
3 votes
2 answers
699 views

Is there a built-in or simple way to convert a file path (module path) to a name argument to be used by the importlib.import_module function? For example: 'path/module.py' -> 'path.module' I ...
0 votes
1 answer
105 views

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

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, ...
23 votes
6 answers
22k views

I am not very familiar with python, I only done automation with so I am a new with packages and everything. I am creating an API with Flask, Gunicorn and Poetry. I noticed that there is a version ...
1 vote
0 answers
90 views

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

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 ...
7 votes
1 answer
3k views

I have an installed package (typically located inside …/lib/pythonX.Y/site-packages/my-package) which contains (among other things) the resources …/my-package/a.txt and …/my-package/b.png. I would ...
2 votes
1 answer
68 views

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 ...
28 votes
2 answers
20k views

Using the deprecated module imp, I can write a custom import hook that modifies the source code of a module on the fly, prior to importation/execution by Python. Given the source code as a string ...
0 votes
1 answer
91 views

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

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

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 ...
26 votes
6 answers
28k views

Suppose I have a module file like this: # my_module.py print("hello") Then I have a simple script: # my_script.py import my_module This will print "hello". Let's say I want to "override" the print(...
0 votes
1 answer
52 views

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

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 ...
4 votes
1 answer
20k views

So I'm working on a project where I should work with Netmiko. I had installed the module using pip3 install netmiko command and it installed successfully. But, when I try to import netmiko module in ...
0 votes
2 answers
299 views

I can't seem to load a tempfile with contents using importlib. It works fine if I have the content saved under a python file (with .py extension), then provide that file path. Is there a way to make ...
2 votes
3 answers
5k views

I tried pip freeze. I need it to output just the modules delimited by newlines, like pip freeze, except without the version number, because I am trying to create an auto upgrader, and want to do pip ...
1 vote
0 answers
52 views

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 ...
3 votes
1 answer
76 views

When doing module = types.ModuleType('mymodule'), we don't get any chance of doing further customization, and we can't override module.__dict__ since it's readonly... The same seems to apply with all ...
5 votes
2 answers
712 views

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

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

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
2 answers
78 views

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() ...
0 votes
1 answer
64 views

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 ...
44 votes
6 answers
17k views

Question The standard library clearly documents how to import source files directly (given the absolute file path to the source file), but this approach does not work if that source file uses ...
-1 votes
1 answer
68 views

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 ...
7 votes
1 answer
2k views

Give something as follows: import importlib module_path = "mod" mod = importlib.import_module(module_path, package=None) print(mod.Foo.Bar.x) where mod.py is: class Foo: class Bar: ...
1 vote
2 answers
4k views

I keep on getting the following error when testing a simple celery example. AttributeError: 'EntryPoints' object has no attribute 'get' using the following CLI command: celery -A ctest worker --...
5 votes
2 answers
3k views

The version in my project is stored in pyproject.toml: [tool.poetry] name = "package_name" version = "1.2.3" # ... I now want to have a __version__ in package_name/__init__.py as ...
2 votes
1 answer
706 views

I am doing a CS course from the MIT Open Courseware website that has been taped in 2016 (course 6.0002 - intro to computational thinking and data science). In one of the problem sets (ps3) the staff ...
12 votes
2 answers
4k views

Without getting confused, there are tons of questions about installing Python packages, how to import the resulting modules, and listing what packages are available. But there doesn't seem to be the ...
0 votes
2 answers
169 views

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 ...
1 vote
0 answers
116 views

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

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
0 answers
30 views

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

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

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 ...
1 vote
1 answer
272 views

I have a project that will be zipped and run from Spark, let's call it "client" project. I would like, on a library imported by a script within this client project, to read some static ...
2 votes
1 answer
2k views

Given some installed package, the following code can be used to print its location on the filesystem: import importlib.resources def print_package_path(pkg): with importlib.resources.path(pkg, &...
0 votes
0 answers
301 views

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
472 views

def get_source(import_name: str) -> Optional[str]: spec = get_spec(import_name) # Now, here. The spec.loader might be one of several values. # But I *know* that it is gonna be importlib....
1 vote
0 answers
604 views

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 (...
1 vote
3 answers
3k views

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
2 3 4 5
9