Error with Import Python Packages(Such as Numpy) - python-3.x

I am using a compute cluster and dont have access to the entire cluster. Therefore, I am trying to locally(in my "home" directory) install packages for python, but I am having problems with importing them from scripts.
I have tried to update my PATH and my PYTHONPATH, set both to ~/.local/lib. There is already a python 3.7.3 module created on the cluster that I am importing. But I dont have access to it to add more packages to it, which is why I have to install locally. Both the path to the module and to my .local directory are in the PATH.
When I use pip to install packages, I get this error: "ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/s1/opt/python-3.7.3/lib/python3.7/site-packages/numpy'
Consider using the --user option or check the permissions." Therefore, I have to install it locally. When I use the --user option, everything seems to install fine. Additionally, the python3.7 version in my .local directory only has python and the site-packages directory, while the python3.7 in the module from the cluster has a lot of other files.
After installing these packages, when I go to my scripts and try to run them, I get these errors.
Traceback (most recent call last):
File "fragment_assignment.py", line 10, in <module>
import numpy as np
File "/s1/snagaraj/.local/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/s1/snagaraj/.local/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was:
No module named _multiarray_umath
As a side note, all the fixes proposed to others who had the error that I found did not work for me.

I suggest creating a virtual environment for your application/development needs and then running in that. In general, virtual environments are a great way to make sure you have all the dependencies you need and do not have a bunch of conflicting issues with other things. Probably the easiest way to get going is with pipenv. Another article on virtual environments and pipenv.
To start, make sure pipenv is installed:
$ pip install --user pipenv
Create a folder for your project and change directory into it (or just cd into your current project directory):
$ mkdir my_project
$ cd my_project
And then start installing the packages you will need from within your my_project directory:
$ pipenv install numpy scipy pandas
or whatever packages you need. Once that operation finishes, you activate your environment by running:
$ pipenv shell
and then whatever you want to do with python. Alternatively, you can just run a script (we'll call it my_script.py) in the environment with:
$ pipenv run python my_script.py

It looks like there may be a missing dependency somewhere. Try installing libatlas3-base to get that file.
sudo apt-get install libatlas3-base

Related

Unable to use pipenv to install: ImportError: cannot import name 'InstallRequirement' from 'pip_shims.shims'

Since a few hours I had issues with installing dependencies via pipenv. I decided to recreate my environment again and start from scratch, using a different python version (3.8.9). I'm now unable to even install any packages at all, and retrieve the following traceback:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pipenv/vendor/requirementslib/models/requirements.py", line 18, in <module>
from pip_shims.shims import (
ImportError: cannot import name 'InstallRequirement' from 'pip_shims.shims' (unknown location)
Any ideas how to resolve this?
For whatever reason, it seems an extended command is required, like so:
python3 -m pipenv install xyz
Before I was justing using
pipenv install xyz

ModuleNotFound : No module named "git"

I was updating my git repo using the below command
python3 helper.py --update SomeName
But it is showing error like:
Traceback (most recent call last):
File "helper.py", line 13, in <module>
import git
ModuleNotFoundError: No module named 'git'
I have installed both Python 3 (3.8.5) and Python 2 (2.7.18).
Following Installing GitPython, a simple pip install GitPython should be enough.
If not, check gitpython-developers/GitPython issue 1051:
Even though I don't know what is causing this I do know that GitPython 2.x is supposed to work with Python 2 and 3, whereas GitPython 3 only works with Python 3.
It's hard to understand what exactly happened here, but when running pip3 install GitPython I am also unable to successfully run import git.
My best guess is that pip installs things outside of the PYTHONPATH, which certainly is surprising
What worked in my case, on Windows, was
python3 -m pip install gitpython
Even after installing GitPython through pip3, the module was not being found, even if I included the appropriate paths. What worked is the command I mention.

I have installed cvlib in python but still can't import it

I installed miniconda and just created a conda environment:
conda create -n my_env python=3.5 anaconda
I am trying to:
import cvlib
But I am getting the error:
ImportError: No module named cvlib
So I have tried to install using:
pip3 install cvlib
This seemed to work successfully, but then when I try to import cvlib I am still getting the ImportError: No module named cvlib error (I have retarted my terminal after the installation).
Is this a problem with my PYTHONPATH not containing the path to the directory that now contains cvlib? If so, how do I find where cvlib is saved so that I can add the path?
Check if the library is in your python directory. Otherwise, make a repl.it account, and install cvlib, and check the functions or the lib name. Maybe try searching a more advanced installation of cvlib.
it might have occurred due to the version of python you installed or due to the directory, you installed.
try uninstalling the current version of python and try installing an older version of python and install it in the directory as shown below:
C:\Users\Rajish\AppData\Local\Programs\Python\Python39
also, select add the path to environment variables while installing
and after that install cvlib and all other required modules and packages
it worked for me.

Add package to Conda Environment from site-packages

this might be a fundamental misunderstanding of Python packages, but I could use some help or directions to the right resources.
I have a egg file in my Python 3.6 site-packages directory, i'll call it package.egg. When I run python from the command line I can use modules from that package. However, when I created a new Pycharm Project and a corresponding Conda environment, I can no longer use that package (for obvious reasons). However, it doesn't seem like just copying package.egg file into the project environments site files.
Is there another process, like unzipping that I have to perform before I can call those modules?
I tried running both pip install ./package.egg and conda install ./package.egg
Thank you.
I was able to resolve this error by reinstalling the package in the context of the new environment. In this case, through the PyCharm terminal I went back to the packages setup.py file and ran that. It installed the package into the correct location in my conda environment.

Install OpenCV correctly on macOS Sierra

I am running on macOS Sierra and I have python 3.5.2 installed on my system.
Now I want to use opencv with python. So here is what I did :-
Installed XCode
Installed CMAKE
Downloaded opencv 3.2 and extracted to my Document directory.
Created a new directory called build inside my opencv directory in Document (obtained from step 3)
Opened CMAKE and configured it to run with source as the opencv directory in Document and target as the build directory within it.
Ran the "make" command from "build" directory
Ran "sudo make install"
All these steps completed successfully. Now I go to IDLE 3.5.2 shell and do "import cv2" and then I get the error "ImportError: No module named cv2"
I tried other solutions like pip3 install opencv-python. It removes the import error but I cannot use this because it doesn't support cv2.imshow() (I kind of need it).
Please help me install and configure opencv correctly.
If you are still stuck you can try by brew, did a fresh install some weeks ago and here is the full process.
As opencv cant support python2 & 3 by default install (read further)
brew edit opencv3
in formula edit this part:
if build.with?("python3") && build.with?("python")
# Opencv3 Does not support building both Python 2 and 3 versions
odie "opencv3: Does not support building both Python 2 and 3 wrappers"
end
by commenting it out ( # at start line).
then:
brew reinstall opencv3 --with-contrib --with-python3 --HEAD
if you have cmake.downloader error you need to edit the brew call and remove the --HEAD flag.
This error is just du to a file missing in the head repository (might be fix soon or not).
Also check your site-package/python version
One issue I found was with then name of the .so library created on install. Try renaming it to cv2.so instead of the longer name it starts with.
On my system it's found here:
/usr/local/opt/opencv/lib/python3.6/site-packages
Also, you can confirm that the above site-packages folder is accessible via sys.path.
>>> import sys
>>> sys.path
If not, you can create a .pth file in one of those directories that contains simply the path name of your site-packages folder above.

Resources