ModuleNotFoundError: No module named 'pymc3' - python

I'm trying to import PyMC3 library in Jupyter Notebook's Python 3 kernel. I've already installed pymc3 initials in Anaconda Prompt (Miniconda3) using:
conda install theano
conda install pygpu
pip install pymc3
And when I try to import it in Jupyter Notebook:
import pymc3 as pm
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-1f817cf6472a> in <module>
1 # PyMC3 for Bayesian Inference
----> 2 import pymc3 as pm
ModuleNotFoundError: No module named 'pymc3'
Any solutions to this problem guys...

You have installed the pymc3 to other python environment that you are using to import it.
Check your python executable location
Inside your script, use
import sys
print(sys.executable)
To get the path to the python executable you are using. Lets say it is C:\python\python.exe.
Install the package
Then, install the package using
<path_to_python.exe> -m pip install pymc3
This is the bulletproof way installing packages with pip, when you have multiple python installations or virtual environments on your system.

Related

Unable to import 'pandas_profiling' module

I have installed 'pandas_profiling' through conda install -c conda-forge pandas-profiling in the base environment. I could see through the conda list that pandas_profiling has been installed correctly (snapshot attached),
When I try to import pandas_profiling I receive ModuleNotFoundError
import pandas_profiling
Traceback (most recent call last):
File "<ipython-input-4-60d2bac64bfc>", line 1, in <module>
import pandas_profiling
ModuleNotFoundError: No module named 'pandas_profiling'
Update: output of import sys; print(sys.path); print(sys.prefix)
['/home/user1/miniconda3/lib/python38.zip', '/home/user1/miniconda3/lib/python3.8', '/home/user1/miniconda3/lib/python3.8/lib-dynload', '', '/home/user1/miniconda3/lib/python3.8/site-packages', '/home/user1/miniconda3/lib/python3.8/site-packages/IPython/extensions', '/home/user1/.ipython']
/home/user1/miniconda3
This is a frequent issue, check out this entry in the FAQ.
Occasionally you will encounter this error if you import a package from the current notebook. It is important to ensure that the pip version is associated with the current Python kernel. That way, the installed packages can be used in the current notebook.
As detailed here, the shell environment and the Python executable are disconnected.
This should work for you
import sys
!{sys.executable} -m pip install pandas-profiling

Jupyter kernel problem caused by pyenv+pipx

I installed two versions of python in pyenv, python3.7.5 and 3.8.6.
I usually use python3.8.6, so it is globally activated (pyenv global 3.8.6).
I also installed pipx by pip install pipx within python3.8.6 to install jupyter lab globally (pipx install jupyterlab), which is located in ~/.local/pipx/venvs/jupyterlab.
It seems that there is one jupyter kernel pre-installed.
$ jupyter kernelspec list
Available kernels:
python3 ~/.pyenv/versions/3.8.6/share/jupyter/kernels/python3
When I execute jupyter lab, this kernel is automatically selected.
However, in jupyter lab, I cannot import numpy, which is installed in python3.8.6.
[1]: import numpy as np
[1]: ---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-0aa0b027fcb6> in <module>
----> 1 import numpy as np
ModuleNotFoundError: No module named 'numpy'
This may be due to a PATH setting.
[2]: import sys
sys.path
[2]: ['~/',
'~/.pyenv/versions/3.8.6/lib/python38.zip',
'~/.pyenv/versions/3.8.6/lib/python3.8',
'~/.pyenv/versions/3.8.6/lib/python3.8/lib-dynload',
'',
'~/.local/pipx/venvs/jupyterlab/lib/python3.8/site-packages',
'~/.local/pipx/shared/lib/python3.8/site-packages',
'~/.local/pipx/venvs/jupyterlab/lib/python3.8/site-packages/IPython/extensions',
'~/.ipython']
~/.pyenv/versions/3.8.6/lib/python3.8/site-packages should be in sys.path. Why is not this path included?
You need to use pipx. Type "pipx inject jupyterlab numpy scipy matplotlib etc etc etc."
This will install those modules into the jupyterlab virtual environment.

xgboost on Sagemaker notebook import fails

I am trying to use XGBoost on Sagemaker notebook.
I am using conda_python3 kernel, and the following packages are installed:
py-xgboost-mutex
libxgboost
py-xgboost
py-xgboost-gpu
But once I am trying to import xgboost it fails on import:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-5-5943d1bfe3f1> in <module>()
----> 1 import xgboost as xgb
ModuleNotFoundError: No module named 'xgboost'
In Sagemaker notebooks use the below steps
a) If in Notebook
i) !type python3
ii) Say the above is /home/ec2-user/anaconda3/envs/python3/bin/python3 for you
iii) !/home/ec2-user/anaconda3/envs/python3/bin/python3 -m pip install xgboost
iv) import xgboost
b) If using Terminal
i) conda activate conda_python3
ii) pip install xgboost
Disclaimer : sometimes the installation would fail with gcc version ,in that case update pip version before running install

Pytorch is installed but is not working on ubuntu 18.04

I am trying to install Pytorch via pip on ubuntu 18.04.I have python 3.6 and my laptop is HP-Pavilion notebook 15
The installation seems to be right because i get the message:
Installing collected packages: torch, torchvision Successfully
installed torch-1.3.1+cpu torchvision-0.4.2+cpu
i run the verification code and it is ok
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
However, when i close the terminal or reboot and try to run he same code i get the error:
Traceback (most recent call last):
File "torch.py", line 2, in
import torch
AttributeError: module 'torch' has no attribute 'rand'
How are you executing the python script? Which python are you using? Maybe you installed the package in a different python version?
Try to set alias to the python you want to use:
alias python=/usr/local/bin/python3.6
Then pip install the package with that python alias you will always be using.
python pip install <package name>
Python now will install the package in the python files with the alias python - heading to files: /usr/local/bin/python3.6
Let me know if the error still occurs!
Install pytorch using pip through the below command:
pip3 install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
for any reference go through the official website of pytorch.
Change your file .py to another name, you named torch.py when you import torch it will call ur torch.py

Error Importing Python Packages into Jupyter

I've been working in Jupyter IPython notebook (using Python 2.7) and haven't had any issues before this importing and installing packages. Most of my packages were installed via Anaconda. Now I'm randomly having problems importing packages that I've always been able to import. I have an example below. Please help. I'm pretty new to Python so I'm completing stuck on what the problem is and how to fix it.
import pandas as pd
ImportError Traceback (most recent call last)
in ()
----> 1 import pandas as pd
C:\Users\IBM_ADMIN\Anaconda2\lib\site-packages\pandas__init__.py in ()
11 "pandas from the source directory, you may need to run "
12 "'python setup.py build_ext --inplace' to build the C "
---> 13 "extensions first.".format(module))
14
15 from datetime import datetime
ImportError: C extension: No module named numpy not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
conda virtual environments will help you a lot. It's good practice to use environments for your projects. And it'll help you avoid causing potential problems with your system's Python.
Try this on the command line:
conda create -n myenv anaconda
source activate myenv
jupyter notebook
That default env will already have pandas; you can install most other things with conda install <package> or, if that doesn't work, pip install <package>.
Running this solved the problem: pip install scipy-0.16.1-cp27-none-win_amd64.whl After doing this, all other packages were able to be re-installed and successfully imported.

Categories