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
Related
When working on some legacy Python package, I noticed there was a sub-package shadowing a module with the same name. Here is a simplified file hierarchy showing the problem:
t/
t/__init__.py
t/u/
t/u.py
t/u/__init__.py
As you can see, there is a python module t/u.py and also a sub-package t/u/ It looks like a standard import statement loads the sub-package:
$ python3
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import t.u
__init__.py
>>>
But, is there a way to import the module t/u.py instead?
Maybe from t import u and from t.u import u works
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
My os is Mac OS, and I have install successfully conda, and cabocha.
and I can import cabocha.analyze (which is a Yet Another Japanese Dependency Structure Analyzer - GitHub Pages ) successfully in terminal.
(wmm_env) A1706-084:wmm_env k.den$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cabocha.analyzer import CaboChaAnalyzer
>>>
But in the pycharm, there is error:
from cabocha.analyzer import CaboChaAnalyzer
ModuleNotFoundError: No module named 'cabocha'
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()
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.