Forum Discussion

andreimap's avatar
andreimap
New Contributor
5 months ago

psycopg2 failes when used from TestComplete but not when from TCs Python directly

There is a Python script that reads from Postgres db.


Notice that I am running it with the TestComplete's own Python. Where the psycopg2 library was added by me.
```
/c/Program\ Files\ \(x86\)/SmartBear/TestComplete\ 15/x64/Bin/Extensions/Python/Python310/python.exe pg_utils.py

```
It has couple of prints that reveals sys.prefix and sys.path
```
sys.prefix: C:\Program Files (x86)\SmartBear\TestComplete 15\x64\Bin\Extensions\Python\Python310

sys.path: [
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\Lib\\site-packages',
'C:\\Tests\\TestComplete\\lib\\postgres',
C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\python310.zip',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\DLLs',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages\\tzdata-2023.3-py3.10.egg',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages\\typing_extensions-4.8.0-py3.10.egg'

```

And script works fine
Connecting to the PostgreSQL database...
['postgres', ...]

BUT: When I try to run it from TestComplete's UI

I got the following output:

```

sys.prefix: C:\Program Files (x86)\SmartBear\TestComplete 15\x64\Bin\Extensions\Python\Python310
sys.path: [
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\Lib\\site-packages',
'C:\\Tests\\TestComplete\\lib\\postgres',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\python310.zip',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\DLLs',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages\\tzdata-2023.3-py3.10.egg',
'C:\\Program Files (x86)\\SmartBear\\TestComplete 15\\x64\\Bin\\Extensions\\Python\\Python310\\lib\\site-packages\\typing_extensions-4.8.0-py3.10.egg'
]


ImportError: cannot import name 'encodings' from 'psycopg2._psycopg' (C:\Program Files (x86)\SmartBear\TestComplete 15\x64\Bin\Extensions\Python\Python310\Lib\site-packages\psycopg2\_psycopg.cp310-win_amd64.pyd)
```
sys.prefix is obviously the same and I have manually copied sys.path to be exactly the same as if I run it directly from Python.

Question: Why it can't import something if it is using same Python.exe and has same sys.path. Is TestComplete forcing script to use some other path, and how can I Debug it.

Thank you!


UPDATE:

It turns out that the error doesn't pop up if I restart TestComplete and run my test for the first time.

If I run something else beforehand or if I try to rerun the test - script produces the error.

I found similar issue [here](https://community.smartbear.com/t5/TestComplete-Questions/Python-fail-to-import-lib-during-the-second-attempt-to-run/td-p/153683) it was not solved but win32com was pointed to be the root cause.

I am importing psycopg2 and using x64 Python. Can I be affected by the same issue. Any meaningful ideas how to figure it out are welcome

6 Replies

    • andreimap's avatar
      andreimap
      New Contributor

      Thank you, I did read it and it helped me to make the script work with TestComplete's Python but not when I run it from TestComplete's UI.

      Are you referring to some specific chapter of that article?

      What is your guess on why it can't work with the library while it's Python can do it? Can I debug it myself?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The section Importing packages, provides the necessary information and the reason for third party packages may not work.

  • andreimap's avatar
    andreimap
    New Contributor

    Additional information:

    It turns out that the error doesn't pop up if I restart TestComplete and run my test for the first time.

    If I run something else beforehand or if I try to rerun the test - script produces the error.

    I found similar issue [here](https://community.smartbear.com/t5/TestComplete-Questions/Python-fail-to-import-lib-during-the-second-attempt-to-run/td-p/153683) it was not solved but win32com was pointed to be the root cause.

    I am importing psycopg2 and using x64 Python. Can I be affected by the same issue. Any meaningful ideas how to figure it out are welcome

  • andreimap's avatar
    andreimap
    New Contributor

    If someone else meets this issue: the ticket was opened and the response in the ticket was exactly same as here, so nothing is going to be improved, need to find a workaround.