why can't import pandas after installed successfully? - python-3.x

I have installed pandas with command 'pip3.4 install pandas'.
Successfully installed pandas python-dateutil pytz numpy six
Cleaning up...
root#hwy:~# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Why can't import pandas in python3.4 after pandas been installed successfully?
root#hwy:/home/debian8# pip3.4 show pandas
---
Name: pandas
Version: 0.17.1
Location: /usr/local/python3.4/lib/python3.4/site-packages
Requires: python-dateutil, pytz, numpy
root#hwy:/home/debian8# echo "import sys; print sys.path"
import sys; print sys.path
root#hwy:/home/debian8# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python3/dist-packages']

Your pandas is installed here:
/usr/local/python3.4/lib/python3.4/site-packages
But this path is not in sys.path.
As a workaround do:
export PYTHONPATH=$PYTHONPATH:/usr/local/python3.4/lib/python3.4/site-packages
and inside this terminal start Python again and do your import of pandas.
If this works, add this line above (export PYTHONPATH...) to your ~/.bashrc or equivalent if you use a different shell for a more permanent solution.

Related

How to check the version of urllib

I created a python virtual env. As urllib is a standard library, I am able to import it directly using import urllib. I want to find out what version of urllib is installed. I tried checking in site-packages but could not find the library there. I tried doing urllib.__version after import urllib. But it's throwing the error AttributeError: module 'urllib' has no attribute '__version__'. How can I find the urllib version?
In python2.7, you can simply get:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05)
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.__version__
'1.17'
>>>
In Python3, urllib was separated to multiple packages, so you need to get the version from urllib.request:
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40)
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.__version__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute '__version__'
>>> import urllib.request
>>> urllib.request.__version__
'3.9'
Even if you try to install it , it will show the output as - which shows it is already installed
root#lancer:# pip install urllib3
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/dist-packages (1.26.2)

Unable to import Pandas in IPython 6.5.0 console (Spyder)

After installing pandas am able to import in cmd as below :
C:\Users\me\Desktop\Django_Project>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when am importing pandas in Spyder in IPython 6.5.0 console I get below error:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
c:\program files (x86)\python37-32\lib\site-packages\ipykernel\parentpoller.py:116: UserWarning: Parent poll failed. If the frontend dies,
the kernel may be left running. Please let us know
about your system (bitness, Python, etc.) at
ipython-dev#scipy.org
ipython-dev#scipy.org""")
In [1] : import pandas
Traceback (most recent call last):
File "<ipython-input-1-38d4b0363d82>", line 1, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
In [2] :
Note: I have installed python in "C:\Users\me\AppData\Local\Programs\Python\Python37\" path and environment variables is set as "C:\Users\me\AppData\Local\Programs\Python\Python37;"
And I installed pandas using PIP
Please suggest the solution to resolve this issue.I have tried reinstalling the pandas package almost 6-7 times.
I think you should open anaconda command prompt by searching it in windows search bar . Now write there "conda install -c anaconda pandas" and try again to run the program

Python3: different behaviour between import and importlib.import_module?

I am unable to dynamically import a module which I have no problem importing in code and I have no idea why.
I have the following:
> ls lib
__init__.py main.py
The init file is empty. The following works:
> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lib.main
>>> lib.main.sayyay()
yay
The following does not work:
> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.import_module("lib.main")
<module 'lib.main' from '/some/path/lib/main.py'>
>>> lib.main.sayyay()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'lib' is not defined
I did read the importlib documentation as well as a couple of answers here on SO, e.g., How to import a module in Python with importlib.import_module and Dynamically import a method in a file, from a string
But what am I missing?
import_module returns the imported module.
Therefore, you need to give the imported module a name and use this just like lib.main
>>> lib_main = importlib.import_module("lib.main")
>>> lib_main.sayyay()

Error importing psycopg2._psycopg when run through cron

When I run the Python interperter directly, I have no problem with importing psycopg2
Python 3.5.1 |Anaconda custom (64-bit)| (default, Dec 7 2015, 11:16:01) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> from psycopg2._psycopg import *
>>>
But if I run import psycopg2 in a script executed through cron, I get the following error. How can I fix that so that it can run though cron as well?
import psycopg2, psycopg2.extras
File "/opt/anaconda3/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named _psycopg
As you can see, I am on Python 3.5 via Anaconda

How to give python access to system wide modules in ubuntu?

I have python 2.7.12 installed in Ubuntu 16.04 (64-bit version). I have modules such as numpy, scipy, sympy etc. installed via pip as well. My problem is, when I open python command line via Terminal and try to import these modules, I get the following error:
$ python
Python 2.7.12 (default, Jul 10 2016, 20:42:07)
[GCC 5.3.1 20160413] 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
>>>
Upon doing some research, I found from this thread that if I open python command line using /usr/bin/python and try importing these modules, I don't get any errors.
$ /usr/bin/python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import scipy
>>> import sympy
>>> import matplotlib
>>> import pandas
>>>
But I would like to know if there is any way I can just type in python from Terminal and import these modules in the python command line? For example, if I write a program like this,
x = 2
print x
y = 5
print y
print x+y
import numpy
import scipy
import sympy
save it in a file named test.py in my desktop and open it using the command /usr/bin/python test.py, I am getting the desired output.
$ /usr/bin/python test.py
2
5
7
But if I try the same with the command python test.py, I get the error again
$ python test.py
2
5
7
Traceback (most recent call last):
File "test.py", line 8, in <module>
import numpy
ImportError: No module named numpy
From what I understand, python doesn't have access to system wide modules since it is installed locally. If so, is there a way to make python global or the modules local to python? I have been trying for the past couple of hours to find a solution but I haven't found anything yet and I am new to Linux. Thanks for your help.
I think the root cause is you have several python binary under $PATH, and your system doesn't use /usr/bin/python by default.
run command which python to see which python is used by default
rename the default python file to something like 'python-2-7-12'
then try to run python test.py again to see if it is resolved.

Resources