How can I install modules in Mac OS Terminal? - python-3.x

I want to install some modules for Telegram Bots and I always get errors like:
File "test.py", line 7, in <module>
import spotipy
ModuleNotFoundError: No module named 'spotipy'
I tried to install spotipy with pip, pip3 and apt.
How is it done? Or generally how do I import modules?
Thank you.

From the spotipy README:
If you already have Python on your system you can install the library simply by downloading the distribution, unpack it and install in the usual fashion:
python setup.py install
You can also install it using a popular package manager with
pip install spotipy
or
easy_install spotipy
Make sure your pip version matches your python version (pip --version and python --version should both be Python3 or Python2).
To import the module, use one of the following statements (see python3 documentation):
import numpy
import numpy as np
from numpy import array

Related

Import "ruamel.yaml" could not be resolved

I am just a beginner in python.
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel.yaml.
I did install the command pip3 install ruamel.yaml and my python list package shows that it's available.
site-packages % pip3 list
Package Version
------------------ ---------
certifi 2021.10.8
charset-normalizer 2.0.11
idna 3.3
pip 22.0.3
pytz 2021.3
PyYAML 6.0
requests 2.27.1
ruamel.yaml 0.17.21
ruamel.yaml.clib 0.2.6
setuptools 60.9.3
urllib3 1.26.8
Whereas when I am trying to import and use it in the python script it is giving me below error.
Import "ruamel.yaml" could not be resolved
Could someone please suggest how to resolve this.?
When Python cannot find a module you get an error looking like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ruamel.yaml
This might be because you are using some non-standard installation/execution environment, or because you try to run this from an editor. Some of those don't understand the concept of namespaces in Python.
It could also be that pip3 is not installing for the python that you are using to run the program. That is easy to check by installing some other package using pip3 and trying to import that.
In either case make sure you test your program running with the python that corresponds to pip3 ( using type pip3 or which pip3 resp type python ), or install using:
python -m pip install ruamel.yaml
and then run the program with python your_prog.py

How to import in python 3?

I am self-learning Python and all the online courses use labs where all libraries are already imported. Whenever I try to import numpy or pandas or any other library I receive this message:
"Traceback (most recent call last):
File "<pyshell#6>", line 1, in
import numpy as np
ModuleNotFoundError: No module named 'numpy'"
What am I doing wrong?
import-error-no-module-named-numpy
ModuleNotFoundError is thrown when a module could not be found
Support for Python 3 was added in NumPy version 1.5.0
you do not install numpy Correctly
pip uninstall numpy
pip3 install numpy
I strongly recommend you use Virtualenv to install it numpy
pip install virtualenv
go to folder of your code use
virtualenv venv
//Windows
venv\Scripts\activate
//Linux
source venv/bin/activate
you can use conda to install numpy
download conda from here coda GUI installer
Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
If you want to install from conda-forge
conda config --env --add channels conda-forge
The actual install command
conda install numpy

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

When importing tensorflow, I get the following error: No module named 'numpy.core._multiarray_umath'

I have installed Ancaconda3 and Tensorflow. When I try to import Tensorflow in python shell I receive the following error:
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
ImportError: numpy.core.multiarray failed to import
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "", line 980, in _find_and_load SystemError:
<class '_frozen_importlib._ModuleLockManager'> returned a result with
an error set ImportError: numpy.core._multiarray_umath failed to
import ImportError: numpy.core.umath failed to import
I am not sure what the problem is as numpy is installed on my system and can be successfully imported in python.
I am using Windows10.
I also had the same issue.
It got resloved once I upgraded the numpy from 1.15.4 to 1.16.1.
If you're using pip:
pip install numpy --upgrade
Numpy that came with Anaconda3 is of version 1.15.4. so i upgraded and it worked.
Side note: if you're also using scikit-image in your script, be aware that numpy 1.16.3 has a conflict with old versions of scikit-image (e.g. you may get ImportError: cannot import name '_validate_lengths'). In that case, pip install --upgrade scikit-image from terminal solved the issue for me.
Kindly check whether you have installed the numpy package from pip. Because if you are running on conda evironment, then all packages need to be downloaded from there.
Please use the below mentioned statement for this purpose
conda install -c anaconda numpy
Also make sure that the numpy version supports the Python version you are using.
You can use two options in python 3.6
Install
py pip -m install numpy==1.14.5
Upgrade
py pip install numpy --upgrade
Note: the version most recently is 1.14.5
I also had this issue with python 3.8.9 and Numpy 1.24.1.
Downgrading to Numpy 1.21.0 fixed the issue.

from ConfigParser import SafeConfigParser ImportError: No module named 'ConfigParser'

I am getting below error:
"from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser''
I am trying to install Mysql-python in python3 but not getting success.
The package is renamed to configparser for PEP 8 compliance and the package you are trying to install doesn't support Python 3.
You can use mysqlclient instead. You can install it using the below command:
pip install mysqlclient
Or one more alternative is there which I won't personally recommend, but just to share it:
# Install configparser
pip install configparser
# Rename it to ConfigParser
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py
After doing the above, you should be able to download MySQL-python without problems.
P.S: Answer is inspired by the answers to this question
In Python 2 this works:
from ConfigParser import SafeConfigParser
Python is case sensitive and in Python 3 the module was renamed to configparser So you would need import like this:
from configparser import SafeConfigParser
Looks like the library you trying to install is for Python 2. You need to get the Python 3 version.

Resources