import statsmodels package returns ModuleNotFound despite correct path - importerror

I am trying to import statsmodels.stats.weightstats.DescrStatsW.tconfint_mean.
I run
from statsmodels.stats.weightstats.DescrStatsW import tconfint_mean
I get the error
ModuleNotFoundError: No module named
'statsmodels.stats.weightstats.DescrStatsW';
'statsmodels.stats.weightstats' is not a package
I have confirmed that I can import other packages from statsmodels with no problems.
I must be using the wrong path, but the docs don't specify any other path to use.

I just figured it out. I will leave this up for progeny.
from statsmodels.stats.weightstats import DescrStatsW
DescrStatsW.tconfint_mean(...)
Not sure why this behaves differently from other python libraries.

Related

ModuleNotFoundError: No module named 'tflite_support.task'

tflite_support's task library is missing. I've install the tflite_support with pip install tflite-support. I've tried using help() function to get the pakage content with help(tflite_support) and got the output 'PACKAGE CONTENTS
_pywrap_codegen
_pywrap_flatbuffers codegen
flatbuffers (package)
metadata
metadata_schema_py_generated
schema_py_generated'. There is no task library inside like how the tflite website shows https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector#run_inference_in_python. I get the same result doing it in my window pc. Am I doing anything wrong or the task library is just missing?
I'm using tflite-support 0.4.1 and it looks like the task module is not supported on Windows:
import flatbuffers
import platform
from tensorflow_lite_support.metadata import metadata_schema_py_generated
from tensorflow_lite_support.metadata import schema_py_generated
from tensorflow_lite_support.metadata.python import metadata
from tflite_support import metadata_writers
if platform.system() != 'Windows':
# Task Library is not supported on Windows yet.
from tflite_support import task
There's also a note about it in the task_library docs.

How to import module up multiple levels from base?

I'm trying to call a module several levels up from pytest run directory. How do I import this module?
I've tried:
sys.path.insert(0, 'path/to/module')
import modulename
Here's the directory structure:
tools
common
modulename.py (contains class A)
functional_test (this is where I'm running pytest; tools/functional_test)
conftest.py
pytest.ini
tests
typea (this is tools/funtional_tests/tests/typea)
tests_typea1.py
Under tests_typea1.py, I want to import class A from modulename.py under tools/common.
Getting the following Errors:
ImportError
ModuleNotFoundError: No module named 'modulename'
Well, after having another person look over my code, I realized that I miss-spelled my the import . Ugh... Note to self: change font style and confirm spelling. :). The above sys.path.insert or sys.path.append with import <modulename> works.

Python module function not defined

I am trying to import a module in my python script and I can't make it work.
So I have my python script: /home/user/pythonscript/oneDir/onescript.py
And I would like to use a script that is a directory higher in hierarchy:
/home/user/pythonscript/common.py
So I did the following at the top of my onescript.py:
import sys
sys.path.insert(1,'/home/user/pythonscript')
import common
In my common.py file, I have a function onecConnect, and when I try to run onescript.py, which uses onecConnect function, I get the following error: nameError: name 'onecConnect' is not defined
Anyone can see what I do wrong or forgot to do?
Thanks
Make sure there are __init__.py in all directories, go to /home/user/pythonscript and run Python code from there. So:
python oneDir/onescript.py
In onescript.py you can do:
from common import onecConnect
The rules are:
Always run a Python script from the highest possible directory (not the deepest into the project).
Always have full import lines, no relative imports.
This keeps the problems away.

can no longer import from ElementTree

For some years alread I have had the following import in my code:
from etree.ElementTree import fromstring, parse, VERSION
Today I made a mistake while moving (in Eclipse/pyDev) a few unrelated sourcefiles to another folder. The folder was not a package and it cost me some cleans, rebuilds and del *.pyc-s to get them found again. That part has been solved, but now, the import above breaks with "unresoved import...". When I remove the etree-prefix, the imports are resolved, but at runtime I get
from ElementTree import fromstring, parse, VERSION
File "C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree\ElementTree.py", line 127, in <module>
from . import ElementPath
ValueError: Attempted relative import in non-package
What is going wrong..?
You should not have been able to.
The import normally would be from xml.etree.ElementTree import ...; the top-level package name is xml.etree, not etree.
It looks as if you added the xml.etree package to the Python sys.path module search path. Don't do that. Remove C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree from your sys.path (or the PYTHONPATH environment variable) and import from the correct top-level package name instead.

Why does importing from a module from the current directory only work when within that directory?

Background
I have a Python project with this directory structure:
py/:
db/ __init__.py run.py
py/db:
handle.py __init__.py util.py
The files are simple enough that I'm not sure I need to post them; nevertheless:
py/run.py
from db.handle import Handle
py/db/handle.py:
import util
class Handle:
def __init__(self, x):
self.x = util.addtwo(x)
py/db/util.py:
def addtwo(x):
return x + 2
If I run handle.py from within the db subdirectory, it imports util without error. However, when I run run.py, handle.py fails with an import error. I can guess that handle.py is being run in the py directory (instead of py/db), and putting a call to os.getcwd() in handle.py confirms this. I can fix this problem using sys.path like so (in run.py):
import sys
sys.path.append("db")
from db.handle import Handle
Question
When importing, from a subdirectory, a module that contains imports to other local modules in that directory, why doesn't Python check the current directory of the module making the import statement? In my example, why doesn't Python check the db first when handle.py contains import statements? Is there a PEP that describes this or is it a behavior with an obvious rationale that I missed?
I thought it might be related to PEP 328:
all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.
but I'm not sure.
Your import is "absolute" and module names is looked for in the PYTHONPATH, and that typically includes the current directory.
If you want to import a module from the same folder that your module is in, you use a relative import:
from . import util
or
from .util import addtwo