Can't find module in python - python

I'm trying to do a linear regression using scipy.stats.linregress(). However when I run my script, i get the error message
AttributeError: 'module' object has no attribute 'stats'*
I'm using the Anaconda python 2.7 distribution which in its documentation says to have the module installed. Anaconda documentation
In the python interactive interpreter, I can import the scipy module, but can't find stats. When I look up its __version__ it says 0.14, which should have the stats module..
I really can't guess why the stats is unavailable..

This error:
AttributeError: 'module' object has no attribute 'stats'
Means what it says. There is no attribute named stats in the scipy module.
Not because no such thing exists on disk, but because no such thing has been imported—because you never even tried to import it.
scipy is a package. As the Python tutorial explains, importing a package doesn't import all of its submodules.
Some packages have a __init.py__ that automatically imports some or all packages.* But that would be a bad idea for scipy, because there are a ton of them, so it would take some time to import all of them, and usually you only need one or two in a given project.
So, you just need to do this:
import scipy.stats
* There are also some cases like os which fake being packages but aren't, so you can use os.path without importing it, or cases like pyobjc that create special placeholder objects for their modules that automatically import the real modules when first accessed.

I get the same error when I import scipy instead of scipy.stats. Have you tried
import scipy.stats
scipy.stats.linregress()

Related

"No module named 'lab_utils_common'"

I was trying to run this program below but it's showing the error "No module named 'lab_utils_common'", what can I do to solve this problem?
import numpy as np
import matplotlib.pyplot as plt
from lab_utils_common import plot_data, sigmoid, dcl
plt.style.use('./deeplearning.mplstyle')
lab_utils_common package is either not publicly available or not actively maintained.
Two possible scenarios:
lab_utils_common is a published package, but not installed. Use pip or conda to install.
lab_utils_common is supposed to be a local module written to lab_utils_common.py, but is missing. See more on modules if interested.

import from lib in python

I import vtt_to_srt converter.I read the instructions.I make the installation.From beginning to this "no problem" but when I import it as manuals describe python can't find the module there.
Installation
pip install vtt_to_srt3
Usage
from vtt_to_srt import vtt_to_srt
path = '/path/to/file.vtt'
vtt_to_srt(path)
Error
ImportError: cannot import name 'vtt_to_srt' from 'vtt_to_srt'
I am a rookie.Sorry for asking this question.
#SakuraFreak's answer was my immediate thought, too, but I ended up investigating this a bit further. It seems like vtt_to_srt stores all its API in the __main__.py file of the module. This file should normally be used when you want to a module using python -m.
This actually makes the module impossible to use the way that the documentation specifies. What I tried, then was:
from vtt_to_srt.__main__ import vtt_to_srt
print(vtt_to_srt)
This results in:
<function vtt_to_srt at 0x000002A0948216A8>
So it seems like this workaround is OK.
I do not know if storing all the module's code in __main__.py is some convention not supported by my version of Python (CPython 3.7.3 on Windows), or if it simply an error. Maybe you should approach the module's owners with this.
Because you're trying to import a function from the module which doesn't exist.
the correct way for this module is:
import vtt_to_srt

How to install project in git repository correctly in python?

Im newbie in python and know i a task from my lecturer to use ANFIS. so I intall anfis in my virtual environent using
pip install anfis
and now I try to import that
import anfis
import membership #import membershipfunction, mfDerivs
import numpy
i got problem
ModuleNotFoundError: No module named 'membership'
so what I must do?
Looks like membership is part of the anfis package, but you are trying to import it as if it were an independent package in itself. Since you haven't installed any package called membership (probably because it doesn't exist as a package), you are getting a ModuleError.
Solution:
First of all, remove the following statement from your code, which is causing the error message:
import membership
Next, since you are already importing the entire anfis package, you can just use a function from the membership module in your code like this:
import anfis
# some code
mfc = membership.membershipfunction.MemFuncs(mf)
Check this link from the anfis GitHub repo for an example on how to use the membership function.
This seems to be a common problem. Try reading through this discussion and see if anything there helps you.
https://github.com/twmeggs/anfis/issues/4

scipy does not load correctly in pycharm

I have imported scipy by this command in pycharm:
import scipy as sp
but as I want to use it in this way for example:
a=sp.special.factorial(5)
I receive an error related to module attribute and I have to import scipy in below way only, to get no error:
from scipy import special
but in another IDE like Spyder both commands runs with no error.
I use anaconda as interpretor in pycharm.
I am not familiar with Spyder. Perhaps they did something extra. But the official way of doing is you have to import scipy.special serapately.
import scipy
help(scipy) # it will give the following help message.
Help on package scipy:
NAME
scipy
DESCRIPTION
SciPy: A scientific computing package for Python
================================================
Documentation is available in the docstrings and
online at http://docs.scipy.org.
Contents
--------
SciPy imports all the functions from the NumPy namespace, and in
addition provides:
Subpackages
-----------
Using any of these subpackages requires an explicit import. For example,
``import scipy.cluster``.

How to fix error "AttributeError: 'module' object has no attribute 'client' in python3?

The following is my code.
import http
h1 = http.client.HTTPConnection('www.bing.com')
I think it's ok.But python give me the following error:
AttributeError: 'module' object has no attribute 'client'.
I wanted to know why and how to fix it.Thanks.
First, importing a package doesn't automatically import all of its submodules.*
So try this:
import http.client
If that doesn't work, then most likely you've got a file named http.py, or a directory named http, somewhere else on your sys.path (most likely the current directory). You can check that pretty easily:
import http
http.__file__
That should give some directory like /usr/lib/python3.3/http/__init__.py or /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/__init__.py or something else that looks obviously system-y and stdlib-y; if you instead get /home/me/src/myproject/http.py, this is your problem. Fix it by renaming your module so it doesn't have the same name as a stdlib module you want to use.
If that's not the problem, then you may have a broken Python installation, or two Python installations that are confusing each other. The most common cause of this is that installing your second Python edited your PYTHONPATH environment variable, but your first Python is still the one that gets run when you just type python.
* But sometimes it does. It depends on the module. And sometimes you can't tell whether something is a package with non-module members (like http), or a module with submodules (os). Fortunately, it doesn't matter; it's always save to import os.path or import http.client, whether it's necessary or not.

Categories