Skip to content

Embedded Python fails to import module files with version_platform extensions #80897

Closed

Description

@elikosan
mannequin
BPO 36716
Nosy @pfmoore, @tjguk, @zware, @zooba, @elikosan

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-05-02.18:38:47.741>
created_at = <Date 2019-04-24.22:07:39.745>
labels = ['extension-modules', 'invalid', 'type-bug', '3.7', 'OS-windows']
title = 'Embedded Python fails to import module files with version_platform extensions'
updated_at = <Date 2019-05-02.18:38:47.741>
user = 'https://github.com/elikosan'

bugs.python.org fields:

activity = <Date 2019-05-02.18:38:47.741>
actor = 'ecosatto'
assignee = 'none'
closed = True
closed_date = <Date 2019-05-02.18:38:47.741>
closer = 'ecosatto'
components = ['Extension Modules', 'Windows']
creation = <Date 2019-04-24.22:07:39.745>
creator = 'ecosatto'
dependencies = []
files = []
hgrepos = []
issue_num = 36716
keywords = []
message_count = 7.0
messages = ['340808', '340809', '340810', '340811', '340846', '341295', '341296']
nosy_count = 5.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'steve.dower', 'ecosatto']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue36716'
versions = ['Python 3.7']

Activity

  1. elikosan commented on Apr 24, 2019

    elikosanmannequin
    MannequinAuthor

    I have an application with embedded python. When trying to import numpy, or any other module that was installed by pip, it fails with error 'ModuleNotFoundError("No module named ...")'. Yet, on the command line python, all works fine. The problem is not in the PATH, as only specific files (those with .<pyver-platform>.pyd extensions, e.g. .cp37-win_amd64.pyd) cannot be found.

    Example1: numpy

    import numpy
    >> ImportError("No module named 'numpy.core._multiarray_umath')

    The line which fails is:
    from . import multiarray
    The file it is trying to load:
    C:\Program Files\Python37\Lib\site-packages\numpy\core_multiarray_umath.cp37-win_amd64.pyd

    Example 2: cv2

    import cv2
    >> ModuleNotFoundError("No module named 'cv2.cv2'")
    The line which fails is:
    from .cv2 import *
    The file it is trying to load:
    C:\Program Files\Python37\Lib\site-packages\cv2\cv2.cp37-win_amd64.pyd
  2. added
    3.7 (EOL)
    extension-modules
    type-bug
    on Apr 24, 2019
  3. zooba commented on Apr 24, 2019

    @zooba
    Member

    Are you sure you don't have the 32-bit version of Python and the 64-bit version of those packages?

  4. elikosan commented on Apr 24, 2019

    elikosanmannequin
    MannequinAuthor

    Yes, and note that the command line python works fine.

  5. zooba commented on Apr 24, 2019

    @zooba
    Member

    Where are you running the embedded Python from, and how have you configured it to include the packages?

    Is your install in Program Files from the full installer? Or have you extracted the embeddable distro there?

  6. elikosan commented on Apr 25, 2019

    elikosanmannequin
    MannequinAuthor

    Hi,

    I have a full install of Python from the installer in:
    C:\Program Files\Python37
    Then, I added libraries using pip.
    Everything works fine from the command line Python.

    Then, I build my C++ application with VisualStudio and link to the Python DLLs. I start Python inside my app using the usual code:

      Py_SetPath(...);
      Py_SetProgramName(...);
      Py_InitializeEx(0);
    
      PyRun_SimpleString("import sys");
      PyRun_SimpleString("print(sys.path)");
      PyRun_SimpleString("print(sys.prefix)");
      PyRun_SimpleString("print(sys.executable)");

    > ['C:\\Program Files\\Python37\\DLLs', 'C:\\Program Files\\Python37\\Lib', 'C:\\Program Files\\Python37', 'C:\\Program Files\\Python37\\Lib\\site-packages']
    > C:\Program Files\Python37
    > C:\Program Files\Python37\python.exe

    So, everything works fine at this point. If I run the following:

      PyRun_SimpleString("import numpy");

    > ImportError("No module named 'numpy.core._multiarray_umath'")

  7. elikosan commented on May 2, 2019

    elikosanmannequin
    MannequinAuthor

    Problem solved:

    Running the following code in the CommandLine Python and in the C++ embedded python clarified the problem:

      import importlib.machinery
      print(importlib.machinery.all_suffixes())
      
    CommandLine Python:
    > ['.py', '.pyw', '.pyc', '.cp37-win_amd64.pyd', '.pyd']

    C++ Embedded Python:

    ['.py', '.pyw', '.pyc', '_d.cp37-win_amd64.pyd', '_d.pyd']

    It shows that the Embedded python is running in Debug mode and therefore adds a _d to the suffix. Yet the numpy or pytorch installed only non-debug files.

  8. elikosan commented on May 2, 2019

    elikosanmannequin
    MannequinAuthor

    I'm closing the issue, see my previous post.

  9. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)OS-windowsextension-modulestype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions