Confusion with virtualenvs and Python packages - python-3.x

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.

Related

Can't install PymuPDF although python Libary have PymuPDF

I tried to install PyMuPDF on Python 3.9 when first I installed by pip install PymuPDF and re-checked by pip list like this"
But when I imported PyMuPDF:
ModuleNotFoundError: No module named 'PyMuPDF'
Next, I tried to install PymuPDF from doc, it said I need install MuPDF first, and install with Wheel, I tried both:
pip install C:\Users\Admin\Desktop/PyMuPDF-1.19.6-cp310-cp310-win_amd64.whl and pip install PyMuPDF-1.19.6-cp310-cp310-win_amd64.whl but reviced error:
yMuPDF-1.19.6-cp310-cp310-win_amd64.whl is not a supported wheel on this platform.
What should i do to install PyMuPDF, thank you all.
PyMuPDF is available with a wheel under all Windows versions - you should have no problem at all.
But please follow this procedure:
Make sure your pip is the current one. This ensures that any changes in supported platform tags are known to pip.
Then install PyMuPDF.
So overall
py -3.10 -m pip install -U pip
py -3.10 -m pip install -U pymupdf
This should simply work!
In your script however you must do import fitz - this is the top-level name of the package.

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

Pip install installs libraries into different locations

I have just started using python and I have set up python3 to be installed in C:\Python37 dir. I have added python3 path to environment variables. When I run python3 -m pip install [package_name] it installs it in C:\Python37\Lib\site-packages. But when I try to install pylint with python3 -m pip install pylint it prints:
Requirement already satisfied: pylint in c:\users\radio\appdata\roaming\python\python37\site-packages (2.3.1)
It's installing it in above mentioned completely different location, and then VS Code complains how pylint is not installed. Why doesn't pip install it in C:\Python37\Lib\site-packages where it installs all the other packages?
It seems you might have multiple python installations on the computer or you are not running CMD with administrator priviledges when using pip.
I would refer you to this thread: windows pip installing libraries in wrong directory which seems to deal with a problem similar to the one you are having.
I would also try checking the environment variables to see the path set for Python if that doesn't work.

Using numpy (and others module)

It's maybe a stupid thing, but I can't resolve it.
I used pip3 to install numpy, scipy... When I rewrote the instruction in command line, it said that
"Requirement already satisfied: numpy in
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
(1.16.2)"
But when I used Python 3.7.2 it said that
"ModuleNotFoundError: No module named 'numpy'"
So that I cannot use numpy actually. I tried also to remove python, and reinstall it via brew. But nothing happen.
Also, I've got a problem with pip. When I do pip3 install --upgrade pip, it answers
You are using pip version 18.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

import ortools in mac

I am trying to run some code using ortools on a python environment. I did not have troubles on a windows machine but I am having problems on mac (10.12.6). if in my virtual environment I run
pip freeze
or
conda list
ortools appears in my list of installed packages. But if I try to use it
ipython
from ortools.linear_solver import pywrapplp
I get an error saying that there's no module named ortools. If I go to
mac/anaconda3/envs/nameenv/lib/python3.6/site-packages I do have a folder called ortools with some python files including pywrapplp. Do you know what I am doing wrong ?
EDIT
following request from coments:
import os
os.getcwd()
returns '/Users/imac'
which ipython
/anaconda3/bin/ipython
Installing ortools is a bit of a headache. It was some days ago, I think I finally made it with
easy_install ortools
I think it is a problem with the path. I guess because I did not install it with conda it does not find the package. I got around writting:
sys.path.append('/anaconda3/envs/env_name/lib/python3.6/site-packages/')
at the begining of my ipynb. That way I can run ortools.
You could have several python interpreter installed (python2 and python3)
So if you want to use it with ipython, which seems to be bind on python3 in your case.
First check if you have pypi package ortools installed
ipython -m pip show ortools
If you got an error it means the package is not installed.
so you can easily install it using:
ipython -m pip install --user ortools
note: We provide Pypi ortools package (64 bits) for Manylinux, Windows and MacOS.
You can also rebuild it from source https://developers.google.com/optimization/introduction/installing/source

Resources