6,058 questions
1
vote
0
answers
159
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, ...
-2
votes
1
answer
86
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
2
answers
160
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 ...
-3
votes
1
answer
81
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 ...
2
votes
1
answer
104
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 ...
0
votes
1
answer
102
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
133
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
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 ...
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 ...
1
vote
0
answers
220
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 ...
2
votes
1
answer
66
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 ...
1
vote
0
answers
75
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
69
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 ...
-2
votes
1
answer
123
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 ...
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
3
answers
164
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 ...
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
vote
1
answer
115
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....
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 ...
1
vote
2
answers
116
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 ...
-1
votes
1
answer
81
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 &...
0
votes
1
answer
188
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
├── ...
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 ...
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 ...
0
votes
0
answers
45
views
How to force Python multiple processes to share the same memory for one import
I am using the CPU version of PyTorch and need to spawn multiple processes with it. However, whenever I do import torch, a large chunk of memory is consumed (roughly 200 MB as of PyTorch 2.4.0+cpu). ...