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
Related
I am running EMR cluster(AWS) but I do not understand how notebook imports packages. I am running PySpark kernel.
import boto3
No module named 'boto3'
Traceback (most recent call last):
ModuleNotFoundError: No module named 'boto3'
print (sys.version) shows
3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
print(sys.executable) shows
/tmp/1594625399736-0/bin/python
I have both Conda and pip3 install of boto3.
How to solve this?
Are you using pyspark? If yes, then you need to install the packages in the spark context. Refer to this AWS document: https://aws.amazon.com/blogs/big-data/install-python-libraries-on-a-running-cluster-with-emr-notebooks/
similarly install any dependency packages if you see module not found error on import. Make sure the versions are compatible.
sc.list_packages()
Package Version
-------------------------- -------
beautifulsoup4 4.9.0
boto 2.49.0
cycler 0.10.0
jmespath 0.9.5
kiwisolver 1.2.0
lxml 4.5.0
matplotlib 3.2.2
mysqlclient 1.4.2
nltk 3.4.5
nose 1.3.4
numpy 1.19.0
pandas 1.0.5
pip 9.0.1
py-dateutil 2.2
py4j 0.10.9
pyparsing 2.4.7
pyspark 3.0.0
python-dateutil 2.8.1
python37-sagemaker-pyspark 1.3.0
pytz 2020.1
PyYAML 5.3.1
setuptools 28.8.0
six 1.15.0
soupsieve 1.9.5
wheel 0.29.0
windmill 1.6
I have boto.
sc.install_pypi_package("boto3")
I've installed python3.8 and the up-to-date version of numpy.
# pip list
Package Version
---------------- ---------
appdirs 1.4.4
certifi 2020.6.20
distlib 0.3.0
filelock 3.0.12
numpy 1.19.0
opencv-python 4.2.0.34
pandas 1.0.5
pip 20.1.1
pipenv 2020.6.2
PyMySQL 0.9.3
python-dateutil 2.8.1
pytz 2020.1
scipy 1.5.0
setuptools 41.2.0
six 1.15.0
virtualenv 20.0.25
virtualenv-clone 0.5.4
Here's c simple code:
import numpy
but python can't import numpy.
Python 2.7.5 (default, Apr 9 2019, 14:30:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/numpy/__init__.py", line 140, in <module>
from . import core
File "/usr/local/lib/python3.8/site-packages/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/usr/local/lib/python3.8/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/usr/local/lib/python3.8/site-packages/numpy/core/overrides.py", line 188
exec(source_object, scope)
SyntaxError: unqualified exec is not allowed in function 'decorator' it is a nested function
>>>
how can I resolve this trouble?
help me. plz..
Try to run the script by using the command python3 script_name.py. The reason for the error could be because script is using python 2.7.5 version.
You may have to check your PYTHONPATH
To print the python search path type this in python shell
import sys
print sys.path
Here you should find the directory containing your numpy library. But if it is missing then manually locate the directory containing numpy library and add it to your python path using this code
sys.path.append('your directory path here')
then try importing again
I am trying to update my matplotlib in virtualenv and now it seems everything crashed :(
what I did till now is basically,
source ~/myenv/bin/activate
pip install -U matplotlib
I had previously matplotlib 1.3 now if I activate py virtual environment and check the matplotlib version it shows 2.1
but I cannot import Axes3D from matplotlib.
>>> import importlib
>>> importlib.import_module('mpl_toolkits.mplot3d').__path__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/nld/python-2.7.6-freya/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/usr/vir_python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/__init__.py", line 6, in <module>
from .axes3d import Axes3D
File "/home/usr/vir_python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 32, in <module>
from matplotlib.cbook import _backports
ImportError: cannot import name _backports
I working on linux platform and using Python 2.7.
Further problem, I saw,
If I just open virtual python, without activating the virtualenv, I get matplotlib 2.1
$:~> myenv/bin/python
Python 2.7.6 (default, Apr 15 2014, 11:17:36)
[GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'2.1.0'
but if I activate the virtual environment and then open the virtual python with I get matplotlib 1.3.1 or old matplotlib.
$:~> source myenv/bin/activate
(vir_python)$:~> myenv/bin/python
Python 2.7.6 (default, Apr 15 2014, 11:17:36)
[GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'1.3.1'
I did the following as per Karthik's suggestion :
First activated the virtual python
(vir_python) $: pip install matplotlib==2.1.0
(vir_python) $: pip freeze
backports.functools-lru-cache==1.4
backports.ssl-match-hostname==3.4.0.2
cycler==0.10.0
imageio==2.1.2
Jinja2==2.7.2
MarkupSafe==0.19
matplotlib==1.3.1
mpi4py==2.0.0
nose==1.3.1
numpy==1.12.0
olefile==0.44
pandas==0.19.2
Pillow==4.0.0
pyparsing==2.0.1
python-dateutil==2.6.0
pytz==2016.10
scipy==0.19.0
six==1.10.0
subprocess32==3.2.7
svg.path==1.1
tornado==3.2
virtualenv==1.11.4
Your virtualenv has a local matplotlib installed.
After sourcing activate, upgrade matplotlib using
pip install matplotlib==2.1.0
or
pip install --upgrade matplotlib
plt.__version__
>> 3.0.0
this is current version of matplotlib in my system.
In the terminal, give the following comments:
$source activate envname
$pip install --upgrade matplotlib
In python prompt,
import matplotlib as plt
plt.__version__
>> 3.0.2
updates the existing version
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.
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.