Python package installation - python-3.x

I am trying to install nibabel through pip:
pip install nibabel
It installs successfully, however, when I write the following code:
import nibabel as nib
I get this error :
ImportError: No module named 'nibabel'
And when I check in the terminal using:
pip show nibabel
It shows that it exists
Author-email: neuroimaging#python.org
License: MIT license
Location: /usr/local/lib/python3.6/site-packages
Requires: numpy, six
This issue happens in both Python 3.x and 2.x.

Here's what you can do:
If you're using Python 2.7, you might need root permission to install the nibabel module.
Try this:
pip install nibabel --user
Hopefully, you should get this Successfully installed nibabel-2.2.1 message.
Then, run your Python 2.7 environment and:
import nibabel as nib
If this doesn't help, you might want to try virtualenv.
Finally, double check if you meet the nibabel requirements e.g. Python 2.7, or >= 3.4.

Related

Python3: No module named dateutil

I am unable to get dateutil installed in my Python code.
from dateutil import tz
ImportError: No module named dateutil
I had date-util installed (Python version is 3.7.3)
> pip3 install python-dateutil
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: python-dateutil in /usr/lib/python3/dist-packages (2.7.3)
I cannot uninstall them (to reinstall). I get the following error
> sudo pip3 uninstall python-dateutil
Not uninstalling python-dateutil at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'python-dateutil'. No files were found to uninstall.
Then, I used the following command to uninstall:
> sudo apt-get remove python3-dateutil #This worked
> pip3 install python-dateutil
This works, but asks me to install cycler, kiwisolver, pyparsing, which I install using pip3. But I still cant get the python code working - has the same error (ImportError: No module named dateutil)
Any suggestions on what's going on?
I found a solution - The python code is being called from a bash script. It worked fine until recently. Recently I installed some other packages that installed python 2.x. So, the python scripts were using Python 2.x rather than 3.x. I had symbolic links to python 3.x, but that didn't help.
Now, I am explicitly use python3 mycode.py to overcome this issue.
I highly recommend to use conda environment instead of pip. See, there are several modules which do not give expected behaviour in pip (like pytorch).
Get the package installed using:
conda install -c conda-forge python-dateutil
returning the question, to get the package running it requires fulfillment of it's dependencies. If you are using python 3.7.3 its obvious that it may lack in some of the features of modern python 3.11 as a result you get the obvious error. The python package python-dateutil must have been configured according to the modern python language version.
Iam using python 3.11.1 and the package gets installed very perfectly. You may install old version of package or upgrade your python language version or use Conda as mentioned before.

Competing Python versions

On my Mac, I have 2 versions of Python running, one from Brew (3.9) and another one (3.8)
When I try to install some data science packages via pip3, they are installed but cannot be used as 3.9 takes over.
python3 --version
Python 3.9.10
which python3
/opt/homebrew/bin/python3
pip3 install pandas
Requirement already satisfied: pandas in /Library/Python/3.8/site-packages
python3
import pandas
ModuleNotFoundError: No module named 'pandas'
pandas is just one example of the many packages I see via pip3 freeze.
What are my options to point to installed versions at Python 3.8 ?
There are different ways to approach this:
One temporary option is to use alias like this:
alias python3=python3.8
You can use directly python3.8 in your terminal.
If you want to install the packages to python3.9 then you can also use pip3.9
You could use:
python3 -m pip install module
which will use pip to install to the python version you just used.
The recommended way is to use:
python3 -m pip install module

Problems importing the oct2py python package

I have installed the oct2py package using the pip command pip install oct2py, set the environement path of python, pip and octave as in the following picture:
However, whenever I try to import it in my python script, I get the following error:
I checked whether the package is installed, it says: Requirement satisfied.
Any ideas of what I could possibly be doing wrong?

Numpy cannot be imported even though it is installed

I am using Linux Mint 19.3 XFCE.
I have installed Numpy through pip3. pip3 was not installed already, and I installed pip3 thorugh apt.
The default version of python3 that came with the OS is 3.6.9. Since I am not supposed to change the default version of Python that comes installed with the OS, I kept that. And I installed a newer version, 3.8.0 with snap.
The command was-
sudo snap install python38
And now, whenever I need to work with the interpreter, I just type python38 into the terminal and get on with it.
I recently installed Numpy with pip3-
pip3 install numpy
and it shows up when I run pip3 freeze
:
It is listed as-
numpy==1.18.1
But when I enter the Python interpreter through typing python38 into my terminal, and type in import numpy, I am shown an error:
import numpy as np
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'numpy'
However, when I try the same with Python 3.6.9, it works. Numpy is improted, works just fine. (This time I enter Python interpreter by typing python3)
Now, how do I permanently solve this? That is, being able to import Numpy when working in Python 3.8 in terminal.
This may be the reason because your pip is configured for the default version of python in the machine(Python 3.6.9 in your case). You may change your default python version.
Or
You could run pip and install your python package for specific python version like
python_version -m pip install your_package
eg
python38 -m pip install numpy

ImportError: No module named nibabel in python 2.7

I am using python 2.7 and I used
pip install nibabel
but I constantly get the error
ImportError: No module named nibabel.
when I type
pip show nibabel
I get this result so it seems that it is installed but still get the aforementioned error when using import nibabel as nib
Name: nibabel
Version: 2.5.0
Summary: Access a multitude of neuroimaging data formats
Home-page: https://nipy.org/nibabel
Author: nibabel developers
Author-email: neuroimaging#python.org
License: MIT License
Location: /usr/local/lib/python3.5/dist-packages
Requires: six, numpy
Required-by:
Here the address shows it is installed in python3 path! How can I address this problem?
Install it using pip2:
pip2 install nibabel
Or explicitly call the pip module for Python 2.7:
python2.7 -m pip install nibabel
for example.

Resources