venv cannot find python library even though I definitely have it installed - python

Im runnig a windows 10 machine, and im getting started on playing with virtual enviorments. I want to run a small fastapi application
I created a venv like this:
python -m venv venv
I then activated the venv, and installed my two dependencies:
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip install fastapi
and
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip install unicorn
Which seemed to work fine, running pip list also seems to indicate that i have what I need:
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> pip list
Package Version
----------------- ---------
beautifulsoup4 4.9.3
certifi 2020.6.20
cffi 1.14.3
chardet 3.0.4
crypto 1.4.1
cryptography 3.1.1
cycler 0.10.0
fastapi 0.63.0
idna 2.10
myModule 1.0.0
Naked 0.1.31
pip 21.0.1
pycparser 2.20
pycrypto 2.6.1
pydantic 1.8.1
PyYAML 5.3.1
requests 2.24.0
setuptools 49.2.1
shellescape 3.8.1
six 1.15.0
soupsieve 2.0.1
starlette 0.13.6
typing-extensions 3.7.4.3
unicorn 1.0.2
urllib3 1.25.10
wheel 0.35.1
But when I run my program it fails with the inmport of the fastapI:
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> python .\main.py
Traceback (most recent call last):
File "C:\Users\Ask\python_Projects\garse_dockerAPI\app\main.py", line 3, in <module>
from fastapi import FastAPI
Also, running python my terminal doesnt wanna do it either:
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastapi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'fastapi'
What's going on? why do i not have the dependencies i need?
EDIT:
In order to find the installation on my machine, I ran 'where python' in my normal terminal:
C:\Users\Ask>where python
C:\Users\Ask\AppData\Local\Programs\Python\Python39\python.exe
C:\Users\Ask\AppData\Local\Microsoft\WindowsApps\python.exe
Which give sme two different paths. I dont really know what to do with this information?
running where pip:
C:\Users\Ask>where pip
C:\Users\Ask\AppData\Local\Programs\Python\Python39\Scripts\pip.exe
When I run it in my venv, the commands output nothing at all:
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> where pip
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app> where python
(venv) PS C:\Users\Ask\python_Projects\garse_dockerAPI\app>

I definitely recommend using python3 -m pip instead of pure pip. It might save you some headache. The reason: when I used pip to install new packages in a venv virtual environment, they ended up in my OS environment! But just using python3 -m pip did the trick.
To make sure that the packages are called from your virtual environment, you can use python3 -m PACKAGE_NAME instead of PACKAGE_NAME. For example, flower didn't work in my virtual environment, but python3 -m flower works with no problem.
You can directly ask python where it is executed from! Try this:
>>> import sys
>>> print(sys.executable)
/PATH/TO/VENV/bin/python
>>> print(sys.version)
'3.10.5 (main, Jun 6 2022, 18:49:26) [GCC 12.1.0]'

Related

"conda install" not installing to pypy

Following this post I created an environment for pypy2.7: conda create -c conda-forge -n pypy2.7 pypy2.7. In the new environment I can now do
$ python -c "import sys; print(sys.executable)"
/usr/bin/python
and
$ pypy -c "import sys; print(sys.executable)"
/home/julian/miniconda2/envs/pypy2.7/bin/pypy
So there is no python in the environment, therefore the system's python gets used, but there is pypy im the environment, which is exactly what we want. Obviously if I try to
$ pypy
Python 2.7.13 (0e7ea4fe15e82d5124e805e2e4a37cae1a402d4b, Dec 28 2017, 20:45:53)
[PyPy 5.10.0 with GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
it does not work.
According to the post mentioned earlier, I should now be able to conda install numpy, however this will do:
conda install numpy
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.8.3
latest version: 4.10.1
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /home/julian/miniconda2/envs/pypy2.7
added / updated specs:
- numpy
The following NEW packages will be INSTALLED:
ca-certificates conda-forge/linux-64::ca-certificates-2020.12.5-ha878542_0
certifi conda-forge/linux-64::certifi-2020.12.5-py39hf3d152e_1
ld_impl_linux-64 conda-forge/linux-64::ld_impl_linux-64-2.35.1-hea4e1c9_2
libblas conda-forge/linux-64::libblas-3.9.0-8_openblas
libcblas conda-forge/linux-64::libcblas-3.9.0-8_openblas
libffi conda-forge/linux-64::libffi-3.3-h58526e2_2
libgfortran-ng conda-forge/linux-64::libgfortran-ng-9.3.0-hff62375_19
libgfortran5 conda-forge/linux-64::libgfortran5-9.3.0-hff62375_19
liblapack conda-forge/linux-64::liblapack-3.9.0-8_openblas
libopenblas conda-forge/linux-64::libopenblas-0.3.12-pthreads_h4812303_1
libstdcxx-ng conda-forge/linux-64::libstdcxx-ng-9.3.0-h6de172a_19
ncurses conda-forge/linux-64::ncurses-6.2-h58526e2_4
numpy conda-forge/linux-64::numpy-1.20.2-py39hdbf815f_0
openssl conda-forge/linux-64::openssl-1.1.1k-h7f98852_0
pip conda-forge/noarch::pip-21.1.1-pyhd8ed1ab_0
python conda-forge/linux-64::python-3.9.2-hffdb5ce_0_cpython
python_abi conda-forge/linux-64::python_abi-3.9-1_cp39
readline conda-forge/linux-64::readline-8.1-h46c0cb4_0
setuptools conda-forge/linux-64::setuptools-49.6.0-py39hf3d152e_3
sqlite conda-forge/linux-64::sqlite-3.35.5-h74cdb3f_0
tk conda-forge/linux-64::tk-8.6.10-h21135ba_1
tzdata conda-forge/noarch::tzdata-2021a-he74cb21_0
wheel conda-forge/noarch::wheel-0.36.2-pyhd3deb0d_0
xz conda-forge/linux-64::xz-5.2.5-h516909a_1
Notice the python conda-forge/linux-64::python-3.9.2-hffdb5ce_0_cpython, so instead of installing numpy to pypy, it installs a new python3.9.
And sure enough after the numpy installation I get
$ python -c "import sys; print(sys.executable)"
/home/julian/miniconda2/envs/pypy2.7/bin/python
and numpy is still not installed for pypy.
How do I get conda install to work on pypy instead of python?
All of this is happening on the WSL (Ubuntu 18.04).
conda does not support pypy2.7

Import a still installed PyPi package in MinGW environment

I have problems doing import of packages I installed via pip3 in a MinGW environment. The module is not found but it is installed.
Buhtzy#Buhtzy MINGW64 ~
$ python3
Python 3.7.3 (default, May 28 2019, 08:37:07) [GCC 8.3.0 64 bit
(AMD64)] on win32 Type "help", "copyright", "credits" or "license" for
more information.
>>> import colorlog
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'colorlog'
>>> exit()
Buhtzy#Buhtzy MINGW64 ~
$ pip3 install colorlog
Requirement already satisfied: colorlog
in /usr/lib/python3.7/site-packages (4.0.2)
It is a MinGW 64bit on a Windows 10 machine.
Some background informations.
The pip3 binary
$ which pip3
/usr/bin/pip3
Which is a script with this content
$ cat /usr/bin/pip3
#!/usr/bin/python3.exe
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
sys.path is
>>> print('\n'.join(sys.path))
C:/Users/Buhtzy/ownCloud/PROJEKTE/naoqi/lib
C:/msys64/mingw64/lib/python37.zip
C:/msys64/mingw64/lib/python3.7
C:/msys64/mingw64/bin
C:/msys64/mingw64/lib/python3.7/lib-dynload
C:/msys64/mingw64/lib/python3.7/site-packages
Python3 also is not able to install pip itself as a module.
$ python3 -m pip install colorlog
C:/msys64/mingw64/bin/python3.exe: No module named pip
I tried to install pip from PyPi
$ pip3 install -U pip
Requirement already up-to-date: pip in /usr/lib/python3.7/site-packages (19.1.1)
I do not understand MinGW but it looks like that Python3 is the default here.
So python is Version 3.7.3 by default.
All PyPi packages I installed with pip3 are able to load via python -m or directly inside the python interpreter shell.

Python Modules installing properly but dont exist in editors

I want to start with I am new to the python language and am learning on my own. My issue is that I am installing the modules that I'm trying to use. The installs are successful but do not exist when I try to import them. I am using Visual Studio Code, Python 3.6, and Anaconda on Windows 10.
I am trying to use pyttsx3, SpeechRecognition and pocketsphinx.
$pip list
This gives me a very large list but does not contain either of my installed modules.
$python -m pip list
Gives me smaller output with them.
Package Version
----------------- ---------
beautifulsoup4 4.6.0
bs4 0.0.1
certifi 2018.4.16
chardet 3.0.4
click 6.7
gTTS 2.0.0
gTTS-token 1.1.1
idna 2.6
pip 10.0.1
pocketsphinx 0.1.3 #Here
pypiwin32 223
pyttsx 1.1
pyttsx3 2.7 #Here
pywin32 223
requests 2.18.4
setuptools 39.1.0
six 1.11.0
SpeechRecognition 3.8.1 #Here
urllib3 1.22
wheel 0.31.0
I know their installed but when I try to use any of the imports.
$python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyttsx3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyttsx3'
I am fine with this being something small on my part due to my lack of knowledge with this language, but I have done a full days worth of research trying to solve this myself.
Edit:
$pip --version
pip 9.0.1 from C:\Users\Keiro\Anaconda3\lib\site-packages (python 3.6)
You have two versions of pip perhaps two different installations of python as well. One with Anaconda and other one by main site.
Stick to one version and if you install in that use that environment in Visual Studio(Or VS Code)
You have installed those in anaconda which the standard installtion of Python wont recognize..
Open Python prompt from Anaconda Navigator >> Choose the environment (Select base for default) >> Open with Terminal [Or Open with Python]
>>python
>>import pyttsx3
In Visual Studio code set the same environment
Ctrl+Shift+P >> Select Python interpreter
Choose the anaconda location.
Volla. Now it should work
I had the same problem and found the answer in an old forum.
Write "pip3" instead of "pip".
So the code is:
pip3 install pyttsx3
(Source: bash: pip: command not found)

Module import error in virtualenv but pip list --local shows it

I'm wondering about the following importError which I can't get ride off. I have a local virtualenv (venv) in which I've installed multiple modules:
(venv) stam#stam:~$ pip list --local
cryptography (1.5.2)
cvxopt (1.1.9)
cycler (0.10.0)
Cython (0.25.1)
matplotlib (1.5.3)
more-itertools (2.2)
ndg-httpsclient (0.4.2)
numpy (1.11.2)
pandas (0.19.0)
paramiko (1.15.2)
Pillow (2.8.1)
pip (8.1.2)
psycopg2 (2.6)
python-apt (0.9.3.12)
python-dateutil (2.5.3)
python-debian (0.1.27)
scipy (0.18.1)
setuptools (28.6.1)
simplejson (3.8.2)
urllib3 (1.9.1)
wheel (0.24.0)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) stam#stam:~$
now starting a python interpreter and trying to import cvxopt (note it is in the list above) gives me a importError. Why is this? I'm using the right python version, I work on the venv so everything should work out of the box.
(venv) stam#stam:~$ python
Python 2.7.9 (default, Aug 13 2016, 16:41:35)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cvxopt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cvxopt
>>>
I'm a bit lost and don't know how exactly I can resolve this issue. It seems the right python version is started, at least the one I've attached to the virtualenv.
Additional outps asked for in the comments
(venv) stam#stam:~$ which python
/usr/bin/python
(venv) stam#stam:~$ which pip
/usr/local/bin/pip
Based on the output from which it seems clear that your python and pip belong to two different instances. So all libraries you install will be installed for some other python instance.
The acute fix is to force pip to run through your specified interpreter:
python -m pip list --local
python -m pip install cvxopt

SciPy in CMD works, but not IDLE

I am running Windows 7 Professional
I have installed Python 3.6
My version was verified by pip --version to give the following:
pip 9.0.1 from C:\ProgramData\Miniconda3\lib\site-packages (python 3.6)
Using PIP I have installed NumPy and SciPy by the following:
pip install numpy
pip install scipy
I can verify the instalation using pip list to show:
cffi (1.9.1)
conda (4.2.13)
cryptography (1
idna (2.2)
menuinst (1.4.4
numpy (1.12.1+m
pip (9.0.1)
pyasn1 (0.1.9)
pycosat (0.6.1)
pycparser (2.17
pyOpenSSL (16.2
pywin32 (220)
requests (2.12.
scipy (0.19.0)
setuptools (27.
six (1.10.0)
wheel (0.29.0)
In the command prompt I can type python, and then import scipy like so:
C:\Users\james.hayek\Desktop>python
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 11:57:41) [MSC v
.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
It would appear as if everything has installed correctly. However, when I start IDLE and type import scipy I get the following error:
>>> import scipy
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import scipy
ModuleNotFoundError: No module named 'scipy'
>>>
Any idea's how I can call/import SciPy in IDLE?
Your Miniconda package has made its own Python SDK default. You need to run your IDLE and add Miniconda's site-packages directory to your regular PYTHONPATH:
import sys
print(sys.path) # to verify that Miniconda is not in this PYTHONPATH
sys.path.append('C:\ProgramData\Miniconda3\lib\site-packages')
To check your path you can equally go to File -> Path Browser

Categories