Forum Discussion

hzheng55's avatar
hzheng55
Occasional Visitor
5 months ago

ImportError: PyO3 modules may only be initialized once per interpreter process.

I am running into an issue while trying to import custom python modules into TestComplete. Specifically, I’m seeing the following error.

To give more info, I believe it worked once, though I could have simply not remembered correctly. I have tried using the steps in this link: https://community.smartbear.com/t5/TestComplete-Questions/Steps-to-Import-Python-Library-in-TestComplete/td-p/202459. I have also tried importing using python build to create a whl. Both of which seem to eventually give this error.

3 Replies

  • getting told to read thru the Testcomplete documentation for Python across so many answers is such a bad answer...
    https://support.smartbear.com/testcomplete/docs/scripting/specifics/python.html 
    this page does not give any real anwers to issues. 
    in this community answer (https://community.smartbear.com/discussions/testcomplete-questions/python-i-have-to-close-and-reopen-testcomplete-after-each-run-or-i-get-a-nonetyp/260098) rraghvani mentions that a certain behaviour is an existing bug , which would be nice to know in the main documentation...

    so If other users get this error, potentially what i describe below may be a way to resolve it 

    ImportError: PyO3 modules may only be initialized once per interpreter process


    this community feature request actual helped me sorta identify what i may have done wrong to get this error 

    https://community.smartbear.com/idea/testxcompletefeaturerequests/python-pip-support/261620

    within my python script code, i was initially trying to follow the documentation where it mentioned we can reference our existing python 

    import sys
    import site

    # Get the user-specific site-packages directory
    user_site_packages = site.getusersitepackages()

    # Replace single backslashes with double backslashes
    user_site_packages = user_site_packages.replace("\\", "\\\\")

    # Log the updated user_site_packages
    Log.Message(user_site_packages)

    # Extract the path up to the Python version
    python_version_path = user_site_packages[:user_site_packages.rfind("\\Python")]

    # Add the user_site_packages and python_version_path to sys.path
    sys.path.insert(0, user_site_packages)
    sys.path.insert(0, python_version_path)

    # Import the desired module
    import simple_salesforce

    user jyurkiw_bmux mentioned that TestComplete may juggle the Python Environment so potentially my code here cause an issue with the Python Environment being picked up as some of the (potentially) same modules were in the TestComplete Python Bin and also in the local computer Bin. so all my script Plays kept failing at this point with the Py03 error (hypothesis...) .


    What resolved my issue was restarting TestComplete and it most likely had stop any Python processes.
    I then removed the code reference to my Local path, and kept copying the modules into the TestComplete folder (which is such a daft process.....)

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      Any third party packages that contain multithreading, asynchronous coroutines, raw byte literals etc may not work correctly -

      Python that comes with TC is, I think, Python Distribution, so it has limited functionality - which is not clearly stated.

      From what I remember, the bug has been there for a very long time. This falls into the category of Importing Packages, "you may experience unexpected errors". So I doubt the bug will get fixed.