0

Try to run this file:

#!flask/bin/python
from flask import Flask, jsonify
#!/usr/bin/python
import psycopg2

And this return:

ImportError: No module named psycopg2

I have file with only psycopg2 and it is work. And I have another file with only flask and it is work too. But when I try to run file with both imports it crush. How to fix it (include flask/bin/python and /usr/bin/python, that flask and psycopg2 work toogether)?

Vlad
  • 87
  • 1
  • 1
  • 6
  • Did you install it? – Guy Jul 25 '19 at 11:18
  • If this module is installed, you should cross-check your Python path. If it does not contain then Python won't look for this folder to find the module. – milanbalazs Jul 25 '19 at 11:23
  • @Guy, Yes, i have edit question – Vlad Jul 25 '19 at 11:28
  • 1
    What is the output if you print your path with `import sys; print(sys.path)` ? Is there a difference in the output with only psycopg2 ? – Jona Jul 25 '19 at 11:34
  • Are you using python3 and venv? Do you have two different python installations? Why are you wrinting another `shebang` line in your file (e.g. `#!/usr/bin/python`) That doesn't do anything, only the first line will be used. – Frieder Jul 25 '19 at 11:34
  • @Frieder, I use './myfile.py, and I want that flask and psycopg2 work together – Vlad Jul 25 '19 at 11:44
  • What is the ouput with `#!flask/bin/python; import sys; print(sys.path)` ? – Jona Jul 25 '19 at 11:48

3 Answers3

1

You have probably two different python installations running.

One in [some path]/flask/bin/python and one in /usr/bin/python. The modules are installed in different environments so you can not use them together.

To fix this install flask module in your 'main' python installation or install psycopg2 in your flask environment.

Frieder
  • 1,208
  • 16
  • 25
  • Should I install psycopg2 into flask/bin? How to do it? flask/bin/ have file named 'python'. If i install psycopg2 into flask/bin i remove last file 'python'? – Vlad Jul 25 '19 at 12:01
  • 1
    I suggest using pip with `--prefix` to install psycopg2. See [this question](https://stackoverflow.com/questions/2915471/install-a-python-package-into-a-different-directory-using-pip) – Frieder Jul 25 '19 at 12:05
1

#! PATH is shebang and Unix/Linux use it for path for executor. (/path/to/script.py insted of python /path/to/script.py)

However, it might help to extend the path in which to look for packages.

Run for your extra enviroment (python bin) this:

# Example:
python -c "import PACKAGE; print(PACKAGE.__path__)"
# Psycopg2
python -c "import psycopg2; print(psycopg2.__path__)"

Output:

['/path/to/some/python/psycopg2']

(My output: /home/usr/miniconda3/envs/free/lib/python3.6/site-packages/psycopg2)

#!flask/bin/python
from flask import Flask, jsonify

import sys
sys.path.insert(0, '/path/to/some/python')  # Without package name
import psycopg2

If you add a path with a package name, you must then be one level down, but beware, __init__.py files can edit paths through the "namespace" so it's not ideal. But you only work with the package itself. Without it, you work with packages that are available in that directory.

Geekmoss
  • 637
  • 6
  • 11
0

You need to install a package module. In terminal execute:

pip install psycopg2

more information in PyPi - PSYCOPG2

  • I have it. I have file with only psycopg2 and it is work. And I have another file with only flask and it is work too. But when I try to run file with both imports it crush. – Vlad Jul 25 '19 at 11:23
  • You only have it locally, with your project there is a python installation shipped, which is used, but doesn't have the global python modules. – Frieder Jul 25 '19 at 11:55