ModuleNotFoundError: No module named 'psycopg2._psycopg' (Ipython) - python

This question has been asked before but none of the answers are satisfactory.
Using Command Prompt on Windows 10, there is no issue importing Psycopg2, which is located in
C:\Users\myusername\AppData\Roaming\Python\Python39\site-packages
For example:
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>>
In the IPython console of Spyder, I get an error:
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 7.19.0 -- An enhanced Interactive Python.
In [1]: import psycopg2
Traceback (most recent call last):
File "<ipython-input-1-7d2da0a5d979>", line 1, in <module>
import psycopg2
File "C:\Users\myusername\AppData\Roaming\Python\Python39\site-packages\psycopg2\__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'
Running sys.path indicates that the location of the package installation is there.
In [4]: sys.path
Out[4]:
['C:\\Program Files\\Spyder\\Python\\python37.zip',
'C:\\Program Files\\Spyder\\Python',
'C:\\Program Files\\Spyder\\pkgs',
'C:\\Program Files\\Spyder\\pkgs\\IPython\\extensions',
'C:\\Users\\myusername\\AppData\\Roaming\\Python\\Python39\\site-packages\\psycopg2',
'',
'C:\\Users\\myusername\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\myusername\\.ipython']
What do I need to do to make this work? It only recently started happening when I reset Windows 10 (uninstalls the operating system and reinstalls it from the cloud, while keeping your files).
This is a standalone installation of Spyder without Anaconda.

As we can see in your path, you have two versions of python installed in your machine.
One points to:
C:\Users\myusername\AppData\Roaming\Python\Python39\site-packages
which is python 3.9.
Another contains the spider code, which is python 3.7.
You just have to install your Psycopg2 in python 3.7 instance.

Related

Make Python module available for all Linux users

I have a Python 2.7 question, if somebody can help.
When we install a Python module using pip, how do we make it available to all users? Please, see the example below (with module faker). The import works when I am root, but doesn’t work when I am ubuntu user.
I have already tried to install using option --system, and also changing umask, as recommended in some articles I have found. Didn’t work so far. Any ideas?
If we run "which python", both users point to the same one.
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
>>>
>>> exit()
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# exit
exit
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$ python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named faker
>>>
Ok, I solved the issue.
In my case, the problematic module was "faker". But, when we install the faker, another additional module is installed as well (in this case - text-unidecode).
Then I uninstalled both modules, ran "umask 022" and re-installed the faker.
This solved the issue for all other users.
Thanks all for the help!

Accessing Python Modules Installed with Brew

I am trying to access "rdkit" module in python, but my python install doesn't seem to find the module. I installed it using brew as suggested here. I included the commands I use to reproduce the problem.
Is a simple fix just adding the "/usr/local/Cellar" to the python path? Symlinc the site-packages to the cellar? Candidly I did not understand the difference between pip and brew when updating my python, so my first thought is that the python path is not correct. Any suggestions greatly appreciated.
X:~\ python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import rdkit Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'rdkit'
X:~\ which python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
X:~\ python3
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
**#:/usr/local/Cellar\** ls
boost/ gettext/ libffi/ numpy/ sip/
boost-python3/ glew/ libidn2/ openblas/ sqlite/
cmake/ glib/ libmpc/ openssl#1.1/ tcl-tk/
ddclient/ glm/ libpng/ pandoc/ tree/
eigen/ gmp/ libtiff/ pcre/ webp/
fontconfig/ graphviz/ libtool/ pkg-config/ wget/
freeglut/ gts/ libunistring/ pyqt/ xz/
freetype/ icu4c/ mmtf-cpp/ python/
gcc/ isl/ mpfr/ qt/
gd/ jasper/ msgpack/ **rdkit/**
gdbm/ jpeg/ netpbm/ readline/
Details:
OS Catalina: 10.15.3

Run scons on Windows 10 environment with Python 2.7 and Python 3

How can I run scons on Windows 10 when Python 3.6.1 and Python 2.7.13 is installed? When typing python into my command window I get the following output:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Since I personally use Python 3.6.x this is fine for me and I do not want to change this.
When typing py -2 into my command window I get the following output:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
scons does not work with Python 3.x. It needs to be executed with Python 2.7.x. How can I "tell" scons to use Python 2.7.x?
When I just type scons into the command prompt I get the following output:
PS C:\dev\projectX> scons
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007050 (most recent call first):
Try Changing line 23 in scons.bat from:
python "%scriptname%" %*
to
py -2 "%scriptname%" %*
Note that work on getting SCons to run under both py 2 and py 3.5+ is mostly done and should be released in the next month (or so)
Before running scons in Powershell, amend the PATH environment variable to put your Python 2.7 installation at the front. Something like:
SET PATH=C:\Python27;%path%

Installing python

I have tried to install Python 3.4 on Windows 7 using the following guide
I have added the following to my Path in system environmental variables
C:\Python34\;C:\Python34\Scripts;
When I open up the Python command line and type the following (as instructed in the tutorial) I receive an error message printed below
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
python --version <- what i type
Traceback (most recent call last):
File "", line 1, in
NameError: name 'python' is not defined
Can anyone help me out?
You need to type python --version from the command line. Not when you are already running python.
As has been mentioned, you need to run your command from the command prompt:
python --version
However, if you are in the python interpreter, you can find the current version with this code:
import sys
print (sys.version)

setting up django with virtualenv on windows7

Im unable to start new project in a virtualenv. This is what i did so far: (fresh windows installation)
1) installed python 2.7 from http://python.org/download/ (not the 64 one)
2) using "set path=%path%;C:\python27" only seems to work for one cmd session, so i added C:\Python27; in my environment variables under advanced system settings, typing python in cmd returns
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
3) downloaded virtualenv.py from http://pypi.python.org/pypi/virtualenv/ ran it with:
C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS>python virtualenv.py testenv
New python executable in testenv\Scripts\python.exe
Installing setuptools....................................done.
Installing pip.........................done.
4) activated it and installed some modules
C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts>activate
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts>
...
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS>pip install -r requirements.txt
...
Successfully installed...
5) tested if it works:
testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.4
>>>
6) Tried to setup a project:
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>django-admin.py startproject testproject
Traceback (most recent call last):
File "C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts\django-admin.py"
, line 2, in <module>
from django.core import management
ImportError: No module named django.core
Path from virtualenv:
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['','C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\DLLs',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\plat-win',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\lib-tk',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\Scripts',
'C:\\Python27\\Lib',
'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages']
I have no idea whats the problem, and how to fix it, also got another question, are those files from c:\python27 supposed to be in my virtualenv path?
Should i attach any other logs? Poke me if so.
Step 6 is where things start to go wrong
Your windows has bound .py(w) files to use Python from c:\python27 directory, not from your virtualenv.
I've written blog entry about virtualenv and win7:
http://djangonautlostinspace.wordpress.com/2012/04/16/django-and-windows/
I agree with jtiai, things go wrong in step 6 because Windows has bound a specific Python. If you did python path/to/django-admin.py startproject it should work.
Please go through the below given tutorial link..
http://ayarshabeer.com/post/50973941605/install-multiple-django-version-using-virtualenvwrapper
Instead of using virtualenv, on Windows I prefer to use Portable Python : http://www.portablepython.com/. You can have several installations on the same machine and switch between them just by setting the path:
set path=d:\python\app\scripts;d:\python\app;%path%
Moreover, it already contains Django. Once you have set up your python environment, you can copy your python directory over to your production server.

Categories

Resources