python-import

Assembling Python module on fly, dynamic import

六眼飞鱼酱① 提交于 2021-02-07 18:14:32
问题 I'm trying to get myself familiar with importlib hooks. I want to implement an ability to directly import non-pythonic files written in other language, and maintain source maps, so raising SyntaxError s with line numbers will still give meaningful stacktraces. My approach to loading foreign files is assembling Pythonic source, then compiling it and executing it in the desired context. I've read in the documentation that implementing importlib.abc.SourceLoader seems to be my choice — however,

Python's __loader__, what is it?

烈酒焚心 提交于 2021-02-06 15:23:06
问题 I've seen the term __loader__ floating around some Python files and I can't find any documentation on it aside from a few brief descriptions about it's purpose, but they still don't provide enough information for me to get a good understanding of it. All I know is that it has something to do with importing modules, other than that I'm completely at a loss. What does it do? When is it used? How can I use it if at all? 回答1: What is __loader__ ? __loader__ is an attribute that is set on an

Python's __loader__, what is it?

£可爱£侵袭症+ 提交于 2021-02-06 15:21:10
问题 I've seen the term __loader__ floating around some Python files and I can't find any documentation on it aside from a few brief descriptions about it's purpose, but they still don't provide enough information for me to get a good understanding of it. All I know is that it has something to do with importing modules, other than that I'm completely at a loss. What does it do? When is it used? How can I use it if at all? 回答1: What is __loader__ ? __loader__ is an attribute that is set on an

Python's __loader__, what is it?

ε祈祈猫儿з 提交于 2021-02-06 15:20:02
问题 I've seen the term __loader__ floating around some Python files and I can't find any documentation on it aside from a few brief descriptions about it's purpose, but they still don't provide enough information for me to get a good understanding of it. All I know is that it has something to do with importing modules, other than that I'm completely at a loss. What does it do? When is it used? How can I use it if at all? 回答1: What is __loader__ ? __loader__ is an attribute that is set on an

Is “from … import …” sometimes required and plain “import …” not always working? Why?

筅森魡賤 提交于 2021-02-05 05:12:13
问题 I always thought, doing from x import y and then directly use y , or doing import x and later use x.y was only a matter of style and avoiding naming conflicts. But it seems like this is not always the case. Sometimes from ... import ... seems to be required : Python 3.7.5 (default, Nov 20 2019, 09:21:52) [GCC 9.2.1 20191008] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import PIL >>> PIL.__version__ '6.1.0' >>> im = PIL.Image.open("test.png") Traceback

Importing module from a directory above current working directory

巧了我就是萌 提交于 2021-02-04 06:31:06
问题 First of all, there are a bunch of solutions on stackoverflow regarding this but from the ones I tried none of them is working. I am working on a remote machine (linux). I am prototyping within the dir-2/module_2.py file using an ipython interpreter. Also I am trying to avoid using absolute paths as the absolute path in this remote machine is long and ugly, and I want my code to run on other machines upon download. My directory structure is as follows: /project-dir/ -/dir-1/ -/__ init__.py -

How to troubleshoot python import error - DLL access denied

对着背影说爱祢 提交于 2021-01-31 07:23:49
问题 I have installed a certain python package (netCDF4), which contains compiled code (extension module). I am running Anaconda and python 3.6 under Windows 10 (x64). When importing the module from console, I get the following error: In [1]: import netCDF4 --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-1-9588a3d4fb24> in <module>() ----> 1 import netCDF4 C:\Program Files\Anaconda3\lib\site-packages\netCDF4\_

How to troubleshoot python import error - DLL access denied

半城伤御伤魂 提交于 2021-01-31 07:22:07
问题 I have installed a certain python package (netCDF4), which contains compiled code (extension module). I am running Anaconda and python 3.6 under Windows 10 (x64). When importing the module from console, I get the following error: In [1]: import netCDF4 --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-1-9588a3d4fb24> in <module>() ----> 1 import netCDF4 C:\Program Files\Anaconda3\lib\site-packages\netCDF4\_

Import error despite __init__.py exists in the folder that contains the target module

天大地大妈咪最大 提交于 2021-01-29 18:05:28
问题 I have the following directory structure inside a virtualenv : /dir_a/dir_b/__init__.py /dir_a/dir_b/module_1.py /dir_a/dir_b/module_2.py /dir_a/dir_c/__init__.py /dir_a/dir_c/module_3.py /dir_a/__init__.py /dir_a/module_4.py Inside module_4.py , I can successfully import module_1.py , module_2.py and module_3.py . On the other hand, I cannot import module_4.py within module_3.py (e.g. import dir_a.module_4 ). It complains: "No module named dir_a.module_4" What am I missing here? Do I need to

Same import, different errors

孤者浪人 提交于 2021-01-29 15:33:12
问题 I am making tests for my code, but I'm facing a strange import error. Notes: I am using VSCode version: 1.46.1 PYTHONPATH is set to project's root Folder structure is this: -app.py -models ---language.py ---category.py -my_tests ---unit ------models ---------category_test.py ---------language_test.py In category_test, I do import "Category" class by using: from models.category import Category In language_test, I do import "Language" class by using: from models.language import Language In