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.
Related
Tried cloning git to Jupyter folder on Windows. No progress.
I have installed SKMultilearn in the same folder as Jupyter.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-22-b987f02e7825> in <module>()
5 return_indicator = 'sparse', allow_unlabeled = False)
6
----> 7 from skmultilearn.adapt import MLkNN
8
9 classifier = MLkNN(k=20)
ModuleNotFoundError: No module named 'skmultilearn'
I faced the same issue and couldn't find a solution. Remember, you must have installed skmultilearn first from cmd, pip install scikit-multilearn. If you have done this and still Jupyter Notebook is saying ModuleNotFoundError: No module named 'skmultilearn', then my solution is for you.
I tried and managed to find an easy solution. I hope that you might have found a solution as this question was asked 3 years ago. I am posting this because many new users will be facing this issue.
Open Jupyter Notebook terminal and go into python.
Type, help('modules'). You will get a list of all packages available to you. 99% chance is that you will not find skmultilearn in that list.
Now, go to command prompt aka cmd.
Type, pip3 install ipykernel --upgrade, then py -3 -m ipykernel install --user, you will get this, Installed kernelspec python3 in C:\Users\MC\AppData\Roaming\jupyter\kernels\python3.
Now type, pip install scikit-multilearn. It will say Requirement already satisfied: scikit-multilearn in c:\python396\lib\site-packages (0.2.0)
Good now type, pip3 install jupyter.
Then, again install pandas and seaborn as your need, pip install pandas and pip install seaborn.
Enjoy, issue resolved. You will not face this error :)
Are you sure that skmultilearn has been correctly installed? It does not depend on the subdirectory where you did the installation.
Open a terminal window in your Jupyter browser, go into python
ipython
and then check out the modules that has been installed:
[1] help('modules')
You will get a list of all packages available to you. skmultilearn should be in the list if it was correctly installed.
pip install scikit-multilearn
pip install scikit-multilearn
I need to use netcdf but do not have install permission for python modules. I have downloaded netcdf-0.1.2.tar.gz from here: https://pypi.python.org/simple/netcdf/ and unzipped the tar ball. I have been following this stack overflow post in an attempt to use the module but have had no luck so far:
(Python) Use a library locally instead of installing it
here is what I have tried:
Installing virtualenv:
I do not have permission to do this
python setup.py install -- user:
again, I don't have permission
running my script with netcdf as my current working directory:
I tried this as well, here are the issues I have run into:
first I went into netcdf-0.1.2 and made a new file called asdf.py
which contains the following:
import netcdf
print("testing")
running python asdf.py gives the following error:
Traceback (most recent call last):
File "asdf.py", line 1, in <module>
import netcdf
File "/.../Downloads/netcdf-0.1.2/netcdf/__init__.py", line 1, in <module>
from netcdf import *
File "/.../Downloads/netcdf-0.1.2/netcdf/netcdf.py", line 1, in <module>
from netCDF4 import Dataset, numpy
ImportError: No module named netCDF4
I'm not sure how to fix this error, any help would be greatly appreciated
in case this is somehow relevant, the version of Linux I am using is 3.2.0-23-generic
also I have numpy installed already
Easest would be to install Anaconda or Miniconda with your user rights.
Anaconda already as netCDF4installed. In case of Miniconda install with:
conda install netcdf4
If you have Python 3 installed, then you will have the venv package in the standard library, so you do not need "virtualenv" to be installed for you separately (as would be the case with Python 2). Instead use python3 -mvenv , in a similar way to how you would use virtualenv, for example:
python3 -mvenv /path/to/my_venv
or to include any non-standard packages already installed on the system:
python3 -mvenv --system-site-packages /path/to/my_venv
After that, you should be able to activate the environment and pip install packages, e.g.
source /path/to/my_venv/bin/activate # for csh use activate.csh instead
pip install netCDF4
Remember to source the activate script at run time as well as installation time:
source /path/to/my_venv/bin/activate
python
and you should then find that in your python session you have the netCDF4 package available, e.g.
import netCDF4
my_dataset = netCDF4.Dataset('myfile.nc')
Of course, substitute the actual path in place of /path/to/my_venv above.
None of this requires any root privileges.
(And as someone else has suggested, another option for you is to use conda.)
I'd also like to highlight that the package is imported using capitals
import netCDF4 as nc
This might not matter on a mac, but for Windows it is key.
I'm trying to install NUMBA on a mac machine (10.12.6).
I've tried with CONDA:
conda install numba
I'm getting this:
Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/lefaa/miniconda2:
#numba 0.35.0 np113py27_6
This seems good, however when I try (using python 2.7):
python -c "from numba import jit"
I'm getting that the numba module isn't installed:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numba
Do I have to configure something else?
It is likely that your path is not set up correctly and the python that conda installed numba into is not the one you are running. If you run which python you should get the one that's in your miniconda2 if everything is working properly. If you get a different python executable then you probably need to set your PATH environment variable. See:
https://conda.io/docs/user-guide/install/index.html#installing-conda-on-a-system-that-has-other-python-installations-or-packages
I solved the issue. Indeed, when installing with conda, conda installs the target package in python associated to conda (i.e, in this python '/Users/lefaa/miniconda2/bin/python'). So to import numba, it is necessary to lunch the python of miniconda.
However, to install numba on the main python (python2.7), one solution will be to install like this '/usr/local/bin/pip2.7 install numba'.
I wrote myself a handy bash script, which solves the task of creating a virtualenv with its own compiled virtualenv and python. It aims at creating a mostly self contained virtualenv, with maybe only native libraries installed in system level if necessary, but installing all python packages and virtualenv and pip and such things inside the virtualenv.
The script can be found here.
I invoke the script as follows:
self_contained_venv.sh \
-n udacity_model_building_and_validation \
-p https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tar.xz \
-v https://pypi.python.org/packages/c8/82/7c1eb879dea5725fae239070b48187de74a8eb06b63d9087cd0a60436353/virtualenv-15.0.1.tar.gz#md5=28d76a0d9cbd5dc42046dd14e76a6ecc \
-d pandas scikit-learn seaborn
Given the required packages for compiling python and virtualenv are installed on the system, the script creates a nice virtualenv. However, when I try to access any installed modules/packages from within the virtualenv, python is not able to find them. To demonstrate this, I'll put some output of commands and code here:
First of all of course I have to activate the virtualenv:
. bin/activate
output: None, works without problem.
Then I print the pythonpath python is aware of:
import sys
for i in sys.path:
print(i)
output:
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python34.zip
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/lib-dynload
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So far so good. Then I try to import a module / package I installed during usage of my bash script: pandas:
python
(IDLE is running)
import pandas as pd
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Another try:
import numpy as np
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
Huh? So none of the packages is available? Lets check pip again:
which pip
output:
alias pip='localpython/bin/pip3.4'
./localpython/bin/pip3.4
Ok, so it's using my local pip.
Check packages:
pip list
output:
numpy (1.11.0)
pandas (0.18.1)
pip (8.1.2)
psutil (4.1.0)
Python-contrib-nbextensions (alpha)
python-dateutil (2.5.3)
pytz (2016.4)
PyYAML (3.11)
setuptools (18.2)
six (1.10.0)
virtualenv (15.0.1)
Hm the packages are there, so why can't python find them? Let's see where those packages are located, simply by trying to remove one:
pip uninstall pandas
output (shortened, because it fills many pages):
Uninstalling pandas-0.18.1:
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas-0.18.1-py3.4.egg-info
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__init__.py
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/__init__.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/_version.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/info.cpython-34.pyc
Aha, so the packages are in the path.
Another attempt on finding out if python looks in the right places:
>>> from distutils.sysconfig import get_python_lib
>>> print(get_python_lib())
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So this one indicates it does not look in the right place, if I understand correctly.
Note:
The script does the following things, which might not be obvious:
compile python with a prefix (local python)
compile virtualenv for the local python
it aliases pip to the local pip of the version of the installed python
it aliases virtualenv to the locally installed one
it installs packages from pypi if specified
it updates the local pip if there is a newer version available
I am a beginner still at writing bash scripts, so I think the structure and logic of the script is fairly easy to understand. It also prints information about success of its operations in the terminal.
Further Notes:
I did not use su or sudo to run the script.
My OS is a Fedora 22, although I think in this case any major distro would work the same way.
OS has been updated recently.
Question: So why can't Python find them / its own packages? (What do I need to change?)
can you try to compare path to pyhton and pip in your script and manually in your system:
which python and which pip?
After that check pip freeze | grep pandas it should return you package ...if no you need to add this package to site-packages folder of you python.
I have an assignment to read excel data in Python. I have Python 2.7 installed. I tried installing xlrd0.8.0 with the following commands in Windows.
C:\Python27\xlrd-0.8.0>python setup.py build
running build
running build_py
creating build
creating build\lib
creating build\lib\xlrd
copying xlrd\biffh.py -> build\lib\xlrd
....
C:\Python27\xlrd-0.8.0>python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
running install_egg_info
Writing C:\Python27\Lib\site-packages\xlrd-0.8.0-py2.7.egg-info
I don't get any error message while installing. I also see xlrd-0.8.0 folder in site-packages in /lib folder...
But when I try to import it, Python is not able to recognize it...
>>> import xlrd
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import xlrd
ImportError: No module named xlrd
Can you suggest how to find the issue?
How to reproduce and fix this error:
Open the python interpreter, try to import xlrt, you get an error:
python
>>> import xlrt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xlrt
1. Install, or make sure pip is installed:
What is the official "preferred" way to install pip and virtualenv systemwide?
2. Install xlrd
pip install xlrd
Protip: If you feel you have to use sudo pip install .... to get this to work then you need to stop and learn why this is dangerous. See: What are the risks of running 'sudo pip'?
Workarounds are to install pip using a certain user: pip install --user myuser ... use your own best judgment here. Make sure the directory your pip is operating in is owned by the user trying to install packages there. Use: chown -R $USER /Library/Python/2.7/site-packages.
3. Test it on the python interpreter:
python
>>> import xlrd
>>> type(xlrd)
<type 'module'>
>>>
Now it is imported it without a problem, xlrd is a python module.
Troubleshooting:
If your PYTHONPATH is not defined, you should define it:
PYTHONPATH=:/home/el/wherever/where_to_find_modules
Resolving issue with xlrd import in Python 2.7
open this link https://bootstrap.pypa.io/get-pip.py and save as get-pip.py and copy this file into C:\Python2.7\
C:\Python2.7\python.exe get-pip.py
After this pip is installed in your system now installing xlrd
C:\Python2.7\python.exe -m pip install xlrd
Open python and import xlrd
import xlrd
it will work.
For me, running python in spyder on a mac, it didn't work even after I installed xlrd using pip, because it was installed to a different location than the one spyder was using. To fix this, I first found where xlrd was installed to:
$pip install xlrd
Requirement already satisfied: xlrd in /usr/local/lib/python2.7/site-packages
Then copied the xlrd folder from there into where Spyder could access it:
$cd /Applications/Spyder.app/Contents/Resources/lib/python2.7/
$cp -r /usr/local/lib/python2.7/site-packages/xlrd.
Then updated the module within spyder, but I'm not sure whether this was necessary. Restarting Spyder might have also worked after making those changes.
If you're using conda,
conda install xlrd
Please add "C:\Python27\Lib\site-packages\" to your PYTHONPATH in your system variables.
If there is no such SYSTEM VARIABLE, please create it:
Right-click the My Computer icon on your desktop and select Properties.
Click the Advanced tab, then click the Environment Variables button.
Under System Variables, click New.
Enter the variable name as PYTHONPATH.
Enter the variable value as C:\Python27\Lib\site-packages\
Click OK.
Click Apply Changes.
I had the same problem, seems like the is better if you export your xlsx to a csv file and then run the following on python
df = pd.read_csv('FileName.csv')
It should work like that. If you're using iPython or even better Jupyter then run df.head() to check if pandas reads your table properly.
Note, I am using Ubuntu
python -m pip install xlrd
For my case I have both python2 and python3 installed, pip install xlrd default install xlrd into the Python3 library (/usr/local/lib/python3.6/site-packages/xlrd/. By specifying python2 for pip install solved my problem.