Using cimports in Pycharm

289 views
Skip to first unread message

Jacob Lynd

unread,
Sep 24, 2021, 11:13:08 PM9/24/21
to cython-users
Hi, all. I'm Jacob, a computational chemistry grad student new to Cython and still relatively inexperienced with programming in general. To make a long story short, the simulations my lab develops are leaking memory, so I want to use malloc() and free() to ensure that arrays that might be missed by the garbage collector can be fully deleted (our code involves matrix multiplications of numpy arrays on the order of 10 MB to simulate each time step of the simulation, so this piles up quickly).

Unfortunately, when I try to import these functions, the line 
"from cython.cimports.libc.stdlib import malloc, free"
is throwing the error
"ModuleNotFoundError: No module named 'cython.cimports.libc.stdlib'"

I'm guessing that my usual python workflow is somehow wrong for Cython, but I don't know where to begin to figure out how, and I was hoping I could ask more experienced programmers for help. For reference, I am using PyCharm with a Conda environment. I have cython 3.0.0a9 installed. In the file where I am trying to import malloc and free, I do "import cython" before anything else and this causes no issues. I copied the import code directly from the cython.readthedocs.io page on memory allocation, so I'm pretty sure it's not a syntax error. I've tried running my code in the console and in the terminal, both throw the same error.

Does anyone know how to get cython.cimports to play nice with Pycharm? I apologize if my question is ignorant, or if I've been making some obvious mistake. Thank you very much for taking the time to read this!

Yours in science,


Jacob Lynd

da-woods

unread,
Sep 25, 2021, 3:43:54 PM9/25/21
to cython...@googlegroups.com
You almost certainly aren't compiling your .py file with Cython (or you're compiling it but the compiled file is ending up in the wrong place).

"import cython" doesn't compile it.

I don't know anything about PyCharm so can't advise there. But you should read https://cython.readthedocs.io/en/latest/src/quickstart/build.html, and probably follow the "setuptools" version.

I'm a bit puzzled where you're leaking memory if you're unable to get malloc - if you aren't doing malloc yourself then you shouldn't need free. So this may not be a productive route to go down.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/d862e169-742d-4fa0-a86f-8ce4c427c1d7n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages