Numpy library functions give an attribute error - python

I wanted to use OpenCV library for my project (in python) so I made a virtual environment in which I installed said library (numpy was automatically installed with it). It worked at first, but when I tried to run a separate python file in the same venv it just redirected it into the numpy library where it showed attribute error in one of its functions. After that, the first file didn't work either.
Error looks like this (the script was run from a command prompt inside VS code):
(env) C:\Users\Python\Small projects>python graph_maker.py
Traceback (most recent call last):
File "graph_maker.py", line 1, in <module>
import cv2
File "C:\Users\Python\Small projects\env\lib\site-packages\cv2\__init__.py", line 11, in <module>
import numpy
File "C:\Users\Python\Small projects\env\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import core
File "C:\Users\Python\Small projects\env\lib\site-packages\numpy\core\__init__.py", line 71, in <module>
from . import numerictypes as nt
File "C:\Users\Python\Small projects\env\lib\site-packages\numpy\core\numerictypes.py", line 594, in <module>
_register_types()
File "C:\Users\Python\Small projects\env\lib\site-packages\numpy\core\numerictypes.py", line 589, in _register_types
numbers.Integral.register(integer)
AttributeError: module 'numbers' has no attribute 'Integral'
Besides that I also got a message that a power shell file (which is inside the virtual environment) can't be run because of restrictions:
& : File C:\Users\Python\Small projects\env\Scripts\Activate.ps1
cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ & "c:/Users/Python/S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I tried running the code inside that terminal and the second error didn't show up anymore, but the first one stayed the same.
I also tried to update pip and reinstall the libraries and when that didn't work I made a new virtual environment, but the error persisted.
Working with libraries and virtual environments is all still new to me so I probably messed up the setup.
Thanks in advance to anyone who tries to help me with this problem.

Related

Error processing line 1 of distutils-precedence.pth (no module named '_distutils_hack')

I'm on Windows 10 and working in a git-bash terminal with anaconda environment. When I open a new terminal, I'm immediately presented with:
Error processing line 1 of C:\Users\e360769\AppData\Local\miniforge3\lib\site-packages\distutils-precedence.pth:
Traceback (most recent call last):
File "C:\Users\e360769\AppData\Local\miniforge3\lib\site.py", line 169, in addpackage
exec(line)
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_distutils_hack'
Remainder of file ignored
This same error comes up any time I run "pip" or "python" on the command line. The command still executes correctly, but only after displaying that error. Note that this started after installing a collection of packages needed for a project I'm working with, and after investigating I found some people attributed this to the setuptools package (I have v41.2.0), but even when I uninstall it, the issue persists. Also note that this is the contents of my distutils-precedence.pth file:
import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'local') == 'local'; enabled and __import__('_distutils_hack').add_shim();
I'm not sure this is actively (for now) causing any issues with executing my python scripts, etc., but I'd like to know what's going on and how to fix this. Would appreciate any help!

Trouble Installing / Running

I installed openmdao via the documentation here (windows 10 plus anaconda): http://openmdao.org/twodocs/versions/latest/getting_started/getting_started.html
If I actually use the [all] flag it seems that pip tries to download every version of the packages so I just went with pip install openmdao
When I try to run the sample from the above link I get this error:
AttributeError: 'Problem' object has no attribute 'model'
I tried re-running in spyder with the same error then tried the first few lines just in the terminal to verify no model attribute existed.
I tried to skip further down into the code with terminal and got some more errors:
prob.driver=om.ScipyOptimizeDriver()
Traceback (most recent call last):
File "<ipython-input-6-8ea598efdab2>", line 1, in <module>
prob.driver=om.ScipyOptimizeDriver()
AttributeError: module 'openmdao.api' has no attribute 'ScipyOptimizeDriver'
I assumed that maybe there is a disconnect with the different versions floating around with openmdao so I then installed the latest non-dev version and tried to run a few included files in that master folder. All the examples I ran had the same error though:
runfile('C:/Users/Vicconius/Anaconda3/OpenMDAO1-master/examples/beam_tutorial.py', wdir='C:/Users/Vicconius/Anaconda3/OpenMDAO1-master/examples')
Traceback (most recent call last):
File "<ipython-input-7-7e855a208cb8>", line 1, in <module>
runfile('C:/Users/Vicconius/Anaconda3/OpenMDAO1-master/examples/beam_tutorial.py', wdir='C:/Users/Vicconius/Anaconda3/OpenMDAO1-master/examples')
File "C:\Users\Vicconius\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "C:\Users\Vicconius\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Vicconius/Anaconda3/OpenMDAO1-master/examples/beam_tutorial.py", line 218, in <module>
top.setup()
File "C:\Users\Vicconius\Anaconda3\OpenMDAO1-master\openmdao\core\problem.py", line 456, in setup
connections = self._setup_connections(params_dict, unknowns_dict)
File "C:\Users\Vicconius\Anaconda3\OpenMDAO1-master\openmdao\core\problem.py", line 234, in _setup_connections
for node in input_graph.nodes_iter():
AttributeError: 'DiGraph' object has no attribute 'nodes_iter'
Any ideas? THANK YOU!!
I can imagine two scenarios:
you happened to create an openmdao folder in your current local directory. You're opening the interpreter or otherwise running python scripts in that same directory. So when you try import openmdao it picks the local folder first --- instead of the installed package. If thats the case, either cd to a different directory or rename/remove that folder.
Something has gone wrong with your install. You somehow have multiple conflicting versions sitting around.
To test, first uninstall with pip:
pip uninstall OpenMDAO.
Then open an interpreter and try
import openmdao
If that somehow works, after you just uninstalled it, that confirms that you somehow have multiple installs.
you need to manually clean up your environment.
inside the interpreter, openmdao.__file__ should tell you where this wonky second install is hiding. You can go delete it manually, and then repeat this import test till you get an ImportError. At that point you know you have found all the rouge installs and you can safely retry a new pip install.

Python code works on interpreter but not when I use pyinstaller

I am creating a bot and I am using libs like pandas, numpy and sklearn and I have also installed all these libraries in a virtual env. The bot works fine on Pycharm but when I use pyinstaller and create a standalone, it's standalone shows this error:
Traceback (most recent call last):
File "PyInstaller\loader\pyiboot01_bootstrap.py", line 167, in __init__
File "ctypes\__init__.py", line 373, in __init__
FileNotFoundError: Could not find module 'C:\Users\vabha\AppData\Local\Temp\_MEI38962\sklearn\.libs\vcomp140.dll' (or one of its dependencies). Try using the full path with constructor syntax.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "chandlerbing.py", line 3, in <module>
from sklearn.pipeline import Pipeline
.
.
.
Console debbug say FileNotFoundError... Let me translate it for you.
vcomp140.dll is missing and need to be used you had it on temporary location and now is not any more. Try also look on this answer or you can try also Auto Py To Exe maybe will work.
You need to have all the libraries installed in the same environment as pyinstaller. You probably installed pyinstaller in a global environment so you should install the libraries in the globa environment.

Sudden Import Error for Previously Working Module

I have a long code with a series of imports. For days now I've been running and rerunning this code, including all the imports, for every small change I make (all variable-related, not touching the modules). I keep doing this when suddenly, one time, I get a ImportError: cannot import name _distributor_init upon running import numpy as np. I am, of course, 100% certain that whatever change I made has nothing to do with this import, especially as this is the first executable line in the file, but regardless I reverted that change yet the error continues.
Following some other threads on StackOverflow I tried reinstalling by doing conda install numpy --force-reinstall, this is because in PyCharm and the conda prompt I can clearly see that numpy is installed and up to date.
According to this thread, I tried running from the console, and according to a comment here I tried commenting out the respective line in the __init__.py file, but somewhat expectantly none of that worked.
I am using Miniconda3 and the environment runs Python 3.7. IDE is PyCharm in which the interpreter is the aforementioned environment.
The full error is:
Traceback (most recent call last):
File "<input>", line 7, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:\Users\OfirL1\Miniconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
ImportError: cannot import name _distributor_init
Aside of having numpy import as usual, which would be nice, I'd love to know what went wrong. This is not the first time that during normal work one module suddenly stops working all of a sudden for no apparent reason.

Python shell working differently on different location in the same machine inside same virtual environment

I have a cloud instance of a Linux machine (openSuSE) with multiple users.
I have created a virtual environment and installed all my required libraries (including Klein).
I have two users "a" and "b".
While logged in as "a" and inside virtualenv, when I open python shell at home directory and type
import klein
it imports normally.
Now when I change directory to
/home/b/
and run the same (open python shell, import klein) while being in the same virtualenv, it gives me an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/a/.local/lib/python3.6/site-packages/klein/__init__.py", line 3, in <module>
from klein._plating import Plating
File "/home/a/.local/lib/python3.6/site-packages/klein/_plating.py", line 16, in <module>
from .app import _call
File "/home/a/.local/lib/python3.6/site-packages/klein/app.py", line 19, in <module>
from twisted.internet import endpoints, reactor
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/endpoints.py", line 58, in <module>
from twisted.protocols.tls import TLSMemoryBIOFactory
File "/home/a/.local/lib/python3.6/site-packages/twisted/protocols/tls.py", line 63, in <module>
from twisted.internet._sslverify import _setAcceptableProtocols
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 158, in <module>
verifyHostname, VerificationError = _selectVerifyImplementation()
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 141, in _selectVerifyImplementation
from service_identity import VerificationError
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/__init__.py", line 7, in <module>
from . import cryptography, pyopenssl
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/cryptography.py", line 16, in <module>
from .exceptions import SubjectAltNameWarning
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/exceptions.py", line 21, in <module>
#attr.s
AttributeError: module 'attr' has no attribute 's'
Command "which python" gives same address at both location which is my virtualenv python address and that should be expected.
But what causes this weird python shell behavior.
Thank you
I solved it and a very shameful reason caused the error.
One of the modules Twisted uses is "attr" module. I had named one of my files attr.py and that is what was causing all the error.
I myself am not deleting this question if moderation has no problem, maybe somebody like me might be stuck at the same situation. It may help them.
Never name your python files same as that of any standard module unless overriding.
Also if your issue persists, then Jean's answer will definitely resolve it.
There can be multiple different Python packages that provide the same Python module. For example, there are at least two packages that provide the attr module:
https://pypi.org/project/attr/
https://pypi.org/project/attrs/
It's possible you've installed the wrong package based on the requirements. You can check what you have installed with pip freeze.

Categories