python-import

Can't access top level python package from sub packages

℡╲_俬逩灬. 提交于 2021-01-29 13:09:49
问题 I have a directory structure like below: chatbot/ __init__.py utils/ __init__.py parser.py nlu/ __init__.py training/ __init__.py module.py I want to access parser.py from module.py . I tried using this line from module.py : from chatbot.utils import parser And I got this error: ModuleNotFoundError: No module named 'chatbot' Any pointers to what I am doing wrong? I am using python3 and trying to run the script as python3 nlu/training/module.py . Thanks in advance! 回答1: I believe the right way

Why use “from <module> import <something>” after “import <module>”?

假如想象 提交于 2021-01-29 12:53:06
问题 I'm trying to understand why in some code it is common practice to import a module first and afterwards importing specific member from the module . import module_a from module_a import member_a Is it just out of pure laziness to not have to write module_a.member_a and instead access member_a directly? Here's the code snippet my question originates from: import spinup from spinup.utils.run_utils import ExperimentGrid from spinup.utils.serialization_utils import convert_json 回答1: Is it just out

Can't access top level python package from sub packages

北城以北 提交于 2021-01-29 12:33:19
问题 I have a directory structure like below: chatbot/ __init__.py utils/ __init__.py parser.py nlu/ __init__.py training/ __init__.py module.py I want to access parser.py from module.py . I tried using this line from module.py : from chatbot.utils import parser And I got this error: ModuleNotFoundError: No module named 'chatbot' Any pointers to what I am doing wrong? I am using python3 and trying to run the script as python3 nlu/training/module.py . Thanks in advance! 回答1: I believe the right way

How do I deal with error while importing in Python

本秂侑毒 提交于 2021-01-29 12:01:19
问题 I have got 2 modules which tend to import each other because they will be using each other in classes. I found in this link which tells to use try/except statement along with imports to deal with circular imports, but still I am getting KeyError instead. The name of module is brand.py which contains the following code: try: from erp.common.models.productwithspecs import ProductWithSpec, ProductWithSpecSchema except ImportError: import sys ProductWithSpec = sys.modules[__package__ + '

Python: import function from an already imported module

喜夏-厌秋 提交于 2021-01-29 01:41:54
问题 Suppose I have a python package my_package which contains a module my_module which contains a function my_function . I was trying to do the following import in python interactive shell: >>> from my_package import my_module >>> my_module.my_function() # => OK >>> from my_module import my_function # => ImportError: No module named my_module >>> from my_package.my_module import my_function # => OK I am quite surprised at the ImportError at the third line above: since the my_module is already

Python: import function from an already imported module

允我心安 提交于 2021-01-29 01:41:13
问题 Suppose I have a python package my_package which contains a module my_module which contains a function my_function . I was trying to do the following import in python interactive shell: >>> from my_package import my_module >>> my_module.my_function() # => OK >>> from my_module import my_function # => ImportError: No module named my_module >>> from my_package.my_module import my_function # => OK I am quite surprised at the ImportError at the third line above: since the my_module is already

Python: import function from an already imported module

守給你的承諾、 提交于 2021-01-29 01:40:42
问题 Suppose I have a python package my_package which contains a module my_module which contains a function my_function . I was trying to do the following import in python interactive shell: >>> from my_package import my_module >>> my_module.my_function() # => OK >>> from my_module import my_function # => ImportError: No module named my_module >>> from my_package.my_module import my_function # => OK I am quite surprised at the ImportError at the third line above: since the my_module is already

Cannot import module in same directory and package

一曲冷凌霜 提交于 2021-01-28 21:00:49
问题 I'm using a from . import module statement to do exactly that: import a local module to my script. The script and module reside in the same folder. # module.py def foo(): print('Foo!') # script.py from . import module module.foo() > ImportError: cannot import name 'module' This should be pretty easy, and doing just import module does work, but as this answer suggests one should, I modified the statements to the former form. The end goal is to have a package, from which I can use things, but

Python package SHAP import

筅森魡賤 提交于 2021-01-28 11:52:39
问题 I installed Python package shap for plotting. conda install -c conda-forge shap After installing, I import shap in jupyter notebook but got error. import shap --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-132-efbb001a1501> in <module> ----> 1 import shap ~\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\__init__.py in <module> 3 __version__ = '0.29.3' 4 ----> 5 from .explainers.kernel import

How to call every function in an imported python module

老子叫甜甜 提交于 2021-01-28 11:44:20
问题 I have a program that I wrote that is creating and maintaining an array and I have another module that I wrote that has functions to manipulate the array. Is it possible to call every function in the imported module without having to hard code every function call? Meaning something like this: #Some way I don't know of to get a list of function objects listOfFunctions = module.getAllFunctions() for function in listOfFunctions: array.function() I want to do this so I don't have to update my