Jupyterlab Package Directory - python-3.x

I'm having trouble importing pypyodbc in a jupyterlab notebook. I'm able to import it in a shell and even "old" jupyter notebooks. I've tried installing it via pip, conda, and manually. When I run !{sys.executable} -m pip install pypyodbc in the notebook it just shows:
Requirement already satisfied: pypyodbc in
c:~\appdata\local\continuum\anaconda3\lib\site-packages\pypyodbc-1.3.3-py3.6.egg
(1.3.3)
Requirement already satisfied: setuptools in
~\appdata\local\continuum\anaconda3\lib\site-packages
(from pypyodbc) (39.1.0)
I can import other packages just fine, but pypyodbc isn't working. What is the default package directory for jupyterlab, and how can I point it to a specific directory to import packages from?

It's possible you have multiple versions of Python running on your computer and Jupyter is selecting a version that does not have that package. To figure out what version of Python is running in your notebook do this inside Jupyter:
import os
import inspect
inspect.getfile(os)
Then you can run pip on that specific version:
python3.6 -m pip install pypyodbc
Alternatively, you can try to uninstall the package and reinstall via pip if it still doesn't work.

Related

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

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

Python package installation

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.

Cannot import scipy in ipython (python 3.4.4)

I have a working setup for Jupyter Notebooks (numpy, scipy, matplotlib, etc.) on workstation 1. I went to set up a second workstation (bash shell on windows 10):
Installed Python 3.4.4 from www.python.org, windows 64 bit.
Upgraded pip: $ python -m pip install --upgrade pip, now its (v 9.0.1)
Installed numpy with pip
Installed scipy with pip
$ pip install scipy
Requirement already satisfied: scipy in c:\python34\lib\site-packages
Requirement already satisfied: numpy>=1.8.2 in
c:\python34\lib\site-packages (from scipy)
See image. When I try to import scipy in ipython I get this '_ccallback_c' error.
I've searched google and this site for importing scipy and this error, but nothing has shown up. Thanks for your help.
As written in the comments, Windows users like myself need to use packages from http://www.lfd.uci.edu/~gohlke/pythonlibs/ (or something alike).
I downloaded the packages I wanted of the correct type (python 3.4, 64 bit windows) and extracted them in my Python>Lib>site-packages folder. Time for data analysis.

Confusion with virtualenvs and Python packages

In my python program (run by a virtualenv using python3.5), I need to use the Pillow library to process an image.
ImportError: No module named 'Pillow'
tells me that Pillow is not installed in the virtualenv.
But, when I run pip install Pillow, I get back:
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages
If the pip I am using is from the virtualenv, then why is it looking in /usr/lib/python3/dist-packages to check if the package is already installed?
Just to make sure, I run type python and type pip to confirm that these 2 programs are from my virtualenv, and they are:
python is hashed (/home/nelson/.virtualenvs/MainEnv/bin/python)
pip is hashed (/home/nelson/.virtualenvs/MainEnv/bin/pip)
sudo was not used when creating the virtualenv (I know because this had already caused problems for me) or when trying to pip install; so where is the flaw in this logic? How can I install Pillow in my virtualenv / How can I import Pillow?
Pillow is a fork of PIL. Hence from PIL import Image. See https://pillow.readthedocs.io/en/4.2.x/handbook/tutorial.html
If you created the virtual environment with --system-site-packages, the virtual environment has access to the global site-packages modules.
You need to re-create the virtual environment without --system-site-packages option if you don't want that.

Resources