I am trying to import a module from a sibling package in Python; following the instructions in this answer. My problem is that the import works… but mypy is saying that it’s a bad import. I’m seeking to understand why mypy is reporting an error, and how to fix it. Directory structure/Code This is a module that I have installed
Tag: python-import
How to use a module as part of a package and as a directly executable script?
Lets say i have a package folder called my_package. This directory contains a file called __init__.py and a module file called my_module.py among further module files. The module my_module.py contains the function do_something_with_file(file_path). The function is used in other parts of the package but it would be practical if it could be called as a command line script that takes
“ModuleNotFoundError” with Azure function Apps using Python
I keep getting the error ModuleNotFoundError: No module named ‘azure’ for line 4 where I import azure.functions as func Below is the code for my init file that has been designed using this tutorial Any help with this would be much appriciated! Answer Did you pip installed the library in your python environment?
Import function from submodule in __init__.py without exposing submodule
I’ working on a Python project with a directory structure similar to this: Where the module bar1 defines the function function1. I would like to have users of my code import function1 (and nothing else) directly from foo, i.e. via from foo import function1. Fair enough, that can be achieved with the following foo/__init__.py: The problem now is that someone
module ‘numpy’ has no attribute ‘dtype’
When importing sklearn datasets eg. I get the error I am not sure why I get this I don’t get this error when running things from a jupyter notebook, which is also weird. Any help on this issue would be greatly appreciated Answer I figured this out. The answer is that the file I was running was named numbers.py. This
‘ModuleNotFoundError’ when trying to import module from imported package
This is my directory structure: man1.py contains the following import statement, which I do not want to change: man1test.py contains the following import statements: I need the second import in man1test.py because man1test.py needs access to a function in man1.py. My rationale behind the first import (Soft) was to facilitate the aforementioned import statement in man1.py. Contrary to my expectation,
Relative imports – ModuleNotFoundError: No module named x
This is the first time I’ve really sat down and tried python 3, and seem to be failing miserably. I have the following two files: test.py config.py config.py has a few functions defined in it as well as a few variables. I’ve stripped it down to the following: config.py test.py I also have an __init__.py However, I’m getting the following
Attempted relative import beyond toplevel package
Here is my folder structure: In test_bash__init__.py I have: while in test_bsa_files.py: Now when I issue: I get: Since ‘Mopy” is in the sys.path and bosh__init__.py is correctly resolved why it complains about relative import above the top level package ? Which is the top level package ? Incidentally this is my attempt to add tests to a legacy project
Python circular imports with inheritance
I have a parent and child class, where a parent’s method returns an instance of the child. Both classes are in separate files classA.py and classB.py. In order to avoid circular imports when I import classA I added the classB import to the end of classA.py (as shown below). Everything worked well and I was able to properly use classA
Relative imports in Python 3
I want to import a function from another file in the same directory. Usually, one of the following works: …but the other one gives me one of these errors: Why is this? Answer unfortunately, this module needs to be inside the package, and it also needs to be runnable as a script, sometimes. Any idea how I could achieve that?