Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalene changes sys.executable #894

Open
weizhuowang opened this issue Dec 7, 2024 · 1 comment
Open

Scalene changes sys.executable #894

weizhuowang opened this issue Dec 7, 2024 · 1 comment

Comments

@weizhuowang
Copy link

Describe the bug
I'm trying to use scalene to profile my existing code. The code works without scalene. The scalene execution always fails at the mujoco - glfw import, error message is "NameError: name 'ctypes' is not defined", despite the import ctypes line works in my conda environment. I traced the error all the way to site-packages - glfw - library.py - line 104. And it appears that sys.executable in args now becomes "/tmp/scalenehc723hj6/python". Is there anyway to designate executable when starting scalene?

`
def _glfw_get_version(filename):
"""
Queries and returns the library version tuple or None by using a
subprocess.
"""
version_checker_source = '''
import sys
import ctypes

    def get_version(library_handle):
        """
        Queries and returns the library version tuple or None.
        """
        major_value = ctypes.c_int(0)
        major = ctypes.pointer(major_value)
        minor_value = ctypes.c_int(0)
        minor = ctypes.pointer(minor_value)
        rev_value = ctypes.c_int(0)
        rev = ctypes.pointer(rev_value)
        if hasattr(library_handle, 'glfwGetVersion'):
            library_handle.glfwGetVersion(major, minor, rev)
            version = (major_value.value,
                       minor_value.value,
                       rev_value.value)
            return version
        else:
            return None

    if sys.version_info[0] == 2:
        input_func = raw_input
    else:
        input_func = input
    filename = input_func().strip()

    try:
        library_handle = ctypes.CDLL(filename)
    except OSError:
        pass
    else:
        version = get_version(library_handle)
        print(version)
'''

args = [sys.executable, '-c', textwrap.dedent(version_checker_source)]
process = subprocess.Popen(args, universal_newlines=True,
                           stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out = process.communicate(filename)[0]
out = out.strip()
if out:
    return eval(out)
else:
    return None

`

To Reproduce
Steps to reproduce the behavior:

  1. pip install mujoco
  2. create script.py: "import mujoco"
  3. scalene script.py

Expected behavior
Import complete without error

Screenshots

Desktop (please complete the following information):

  • OS: ubuntu 22.04

If you have not yet tried with the repository version (python3 -m pip install git+https://github.com/plasma-umass/scalene), please try that before reporting.

Issue still exists in repo version

Additional context
Add any other context about the problem here.

@tangerine1202
Copy link

tangerine1202 commented Feb 17, 2025

Also encountered this issue with glfw==2.7.0.
A quick workaround works for me is to add import ctypes in the def get_version as:

def get_version(library_handle):
        """
        Queries and returns the library version tuple or None.
        """
        import ctypes # add this
        major_value = ctypes.c_int(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants