So I have been having some trouble with this. I need to use numpy so that I can use OpenCV and so I installed Miniconda (Not Miniconda3 because we are working in Python 2.7) and I installed numpy with conda install numpy and it worked because when I run conda list I see that it is there:
Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\joe30_000>conda list
# packages in environment at C:\Users\joe30_000\Miniconda:
#
conda 3.17.0 py27_0
conda-env 2.4.2 py27_0
menuinst 1.0.4 py27_0
numpy 1.9.3 py27_0
pip 7.1.2 py27_0
pycosat 0.6.1 py27_0
pycrypto 2.6.1 py27_3
python 2.7.10 0
pyyaml 3.11 py27_2
requests 2.7.0 py27_0
setuptools 18.3.2 py27_0
wheel 0.26.0 py27_1
C:\Users\joe30_000>
And as you can see it is installed for Python 2.7. However, when I go to the python command line and try import numpy I get the error that no module exists:
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
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
>>>
Why is this happening? Do I have to somehow sync Conda with Python so that Python is using the modules that Miniconda has installed?
So thanks to #Bubbafat, I found the solution and I want to post it in case anyone else has problems. Like Bubbafat said, conda has its own version of the Python interpreter. It is located in the Miniconda directory (It's called "Python.exe"). If you are using an IDE you need to switch the interpreter to use this version of Python rather than the default one you may have installed on the internet from the Python website itself. I hope this was helpful.
Related
Here's what I'm running into:
(.venv) PS C:\Users\<redacted>\onedrive\dev\python\code\kb4> py kb4.py
Traceback (most recent call last):
File "C:\Users\hanawayc\onedrive\dev\python\code\kb4\kb4.py", line 7, in <module>
from dateutil.parser import parse
ModuleNotFoundError: No module named 'dateutil'
However:
(.venv) PS C:\Users\<redacted>\onedrive\dev\python\code\kb4> py
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from dateutil.parser import parse
>>> quit()
The import statements in kb4.py are as follows:
from dateutil.parser import parse
from os import getcwd, mkdir
from os.path import exists
from sys import exit, stdout
from time import sleep, strftime
import json
import logging
import pandas as pd
import requests
If it matters, here is my pip list:
Package Version
------------------ ---------
astroid 2.6.5
backcall 0.2.0
certifi 2021.5.30
charset-normalizer 2.0.3
colorama 0.4.4
debugpy 1.4.1
decorator 5.0.9
idna 3.2
ipykernel 6.0.3
ipython 7.25.0
jedi 0.18.0
jupyter-client 6.1.12
jupyter-core 4.7.1
lazy-object-proxy 1.6.0
numpy 1.21.1
pandas 1.3.1
parso 0.8.2
pickleshare 0.7.5
prompt-toolkit 3.0.19
Pygments 2.9.0
pylint 2.9.6
python-dateutil 2.8.2
pywin32 301
pyzmq 22.1.0
requests 2.26.0
setuptools 56.0.0
six 1.16.0
toml 0.10.2
tornado 6.1
traitlets 5.0.5
urllib3 1.26.6
wcwidth 0.2.5
wrapt 1.12.1
I have tried:
pip install python-dateutil --force-reinstall
uninstalling and reinstalling python-dateutil
both with no success.
Any ideas on what else I might try?
Your question is indeed interesting! I could reproduce your scenario and obtain the same output, using the pandas library for testing:
(myvenv) RANGO#RANGO-PC D:\myvenv
> py testpandas.py
Traceback (most recent call last):
File "D:\myvenv\testpandas.py", line 3, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
(myvenv) RANGO#RANGO-PC D:\myvenv
> py
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 pandas as pd
>>>
You are using py to launch Python, so visiting the Python Launcher's documentation I found a clue of what may be happening in your situation.
You say that there are 6 lines of comments before the import that fails. Maybe one of those comments is a shebang line which redirects to another Python installation on your environment.
My test script is the following:
#! C:\msys64\mingw64\bin\python
#
import pandas as pd
s = pd.Series([1, 3, 5, 12, 6, 8])
print(s)
There you have one shebang line and a comment line before the import line. The shebang line is forwarding the execution and environment to another python installation different than the "default" one, as the following test shows.
(myvenv) RANGO#RANGO-PC D:\myvenv
> C:\msys64\mingw64\bin\python -V
Python 3.9.7
(myvenv) RANGO#RANGO-PC D:\myvenv
> py -V
Python 3.9.0
I'm using conda and pip to manage my packages.
In my environment.yml, I have the following
- LOTS OF PACKAGES
- ...
- ...
- pip:
- pyarrow==0.16.0
So pyarrow should be a specific version - 0.16.
I conda activate into that environment. And when I do a pip freeze or pip show, the version agrees. It's supposed to be 0.16
(CONDA) $ pip show pyarrow
Name: pyarrow
Version: 0.16.0
Summary: Python library for Apache Arrow
Home-page: https://arrow.apache.org/
Author: None
Author-email: None
License: Apache License, Version 2.0
Location: /home/<USRER>/anaconda3/envs/CONDA/lib/python3.6/site-packages
Requires: numpy, six
But when I fire up python, import the library, and try get the version, it's a different version.
(CONDA) $ python
Python 3.6.6 |Anaconda, Inc.| (default, Oct 9 2018, 12:34:16)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyarrow
>>> pyarrow.__version__
'0.12.0'
>>> pyarrow.__file__
'/home/<USER>/anaconda3/envs/CONDA/lib/python3.6/site-packages/pyarrow/__init__.py'
I don't understand how that's possible. I would expect the version to agree, but for some reason python insists that pyarrow is a different version.
Now I suspect my entire conda environment is bad. Shouldn't the version that I get in python agree with pip freeze?
pyarrow does some funny stuff with the __version__ variable. It is generated using setuptools-scm.
So maybe the conda release you installed is broken for some reason. Maybe try to install it manually by cloning it and installing it with pip from the cloned folder without passing through conda and see if you get a different result.
I beleive you can do this automagically by running:
pip install "git+https://github.com/apache/arrow#apache-arrow-0.16.0#egg=pyarrow&subdirectory=python"
Solved: Learn about virtual environments.
pip install virtualenv
Problem: I get a ModuleNotFoundError for psycopg2 in python3, though it's successfully installed via pip3. (I posted short code to summarize from the terminal, but the errors are of course bugging me with .py scripts I'm trying to run.)
Python3 packages:
macs-MacBook-Air-2% pip3 list
Package Version
---------- -------
pip 19.2.3
psycopg2 2.8.4
setuptools 41.4.0
wheel 0.33.6
Python3 psycopg2 error:
macs-MacBook-Air-2% python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'psycopg2'
I've only done:
pip3 install psycopg2
Extra notes:
As a side note, everything works fine when I run 2.7. (I used pip install psycopg2.)
macs-MacBook-Air-2% python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.__version__
'2.8.4 (dt dec pq3 ext lo64)'
>>>
python 2.7 packages:
macs-MacBook-Air-2% pip list
DEPRECATION: ...2.7 end of life notice..
Package Version
-------------------------------------- -----------
altgraph 0.10.2
astroid 1.6.6
...more
psycopg2 2.8.4
...more
zope.interface 4.6.0
I'm very new to coding, and I searched this error. But, I did not find results that made sense to me including both a python3 error + successful python3 installation.
This could be because your default python installation is Python 2. I think you should create a virtual environment and the install psycopg2 on it. This way you will use pip3 and have isolated dependencies that won't generate conflicts with other versions (and maybe corrupt your system):
python3 -m venv ~/.environments/test
source ~/.environments/test/bin/activate
pip install psycopg2
I used pip3 to install tensorflow.. And after installation, I can find it in
/usr/local/lib/python3.6/site-packages
Besides, I use pip3 list and find it on the list:
vincent#ubuntu:/usr/local/lib/python3.6/site-packages/tensorflow$ pip3 list
backports.weakref (1.0rc1)
bleach (1.5.0)
html5lib (0.9999999)
Markdown (2.6.8)
numpy (1.13.1)
pip (9.0.1)
protobuf (3.3.0)
setuptools (28.8.0)
six (1.10.0)
tensorflow (1.2.1)
Werkzeug (0.12.2)
wheel (0.29.0)
But but I failed to import it in python3.6
vincent#ubuntu:/usr/local/lib/python3.6/site-packages/tensorflow$ python
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'
>>>
What's wrong with it?
I ran it in Linux14.04(x64) in VMware
There may be multiple versions of python installed. You can do the following to ensure that tensorflow is installed for the version you are using -
python -mpip install tensorflow
Here I assume that you are using python prompt to run python. If you are using a different prompt just use that in place of python.
I created an environment called imagescraper and installed pip with it.
I then proceed to use pip to install a package called ImageScraper;
>>activate imagescraper
[imagescraper]>>pip install ImageScraper
Just to ensure that I have the package successfully installed:
>>conda list
[imagescraper] C:\Users\John>conda list
# packages in environment at C:\Anaconda2\envs\imagescrap
#
future 0.15.2 <pip>
imagescraper 2.0.7 <pip>
lxml 3.6.0 <pip>
numpy 1.11.0 <pip>
pandas 0.18.0 <pip>
pip 8.1.1 py27_1
python 2.7.11 4
python-dateutil 2.5.2 <pip>
pytz 2016.3 <pip>
requests 2.9.1 <pip>
setproctitle 1.1.9 <pip>
setuptools 20.3 py27_0
simplepool 0.1 <pip>
six 1.10.0 <pip>
vs2008_runtime 9.00.30729.1 0
wheel 0.29.0 py27_0
Before I launch Jupyter notebook, just to check where we are getting the path from:
[imagescraper] C:\Users\John>python
Python 2.7.11 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:58:36) [MSC
v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import sys
>>> sys.executable
'C:\\Anaconda2\\envs\\imagescraper\\python.exe'
>>> import image_scraper
Seems ok, so I proceed to launch Jupyter notebook using
[imagescraper]>>jupyter notebook
Within the notebook I created a new book and when i tried the same;
import image_scraper
I am returned with:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-6c2b65c9cdeb> in <module>()
----> 1 import image_scraper
ImportError: No module named image_scraper
Doing the same to check the paths within Jupyter notebook, I get this;
import sys
sys.executable
'C:\\Anaconda2\\python.exe'
Which tells me that it is not referring to the environment where I installed the modules in.
Is there a way I can ensure that my notebooks all refer to its own env packages?
Here are two possible solutions:
You can register a new kernel based on your imagescraper environment. The kernel will start from the imagescraper environment and thus sees all its packages.
source activate imagescraper
conda install ipykernel
ipython kernel install --name imagescraper
This will add a new kernel named imagescraper to your jupyter dashboard.
Another solution is to install jupyter notebook into the imagescraper environment and start jupyter from the enviroment. This requires activating imagescraper whenever you start jupyter notebook.
source activate imagescraper
conda install notebook
jupyter notebook