Hi, thanks for reporting an issue with Cursor.
Before you report this, we’d appreciate if you can search this forum to see if this issue has already been reported.
If you have done so, please check this box.
on
Describe the Bug
For Python when calling a nested test script as “main” and trying to import a module from a parent directory the terminal window does not pick up on new paths added to the sys path list.
Steps to Reproduce
Directory structure
src/
src/tests/unit/test.py
src/target_directory/target_module.py
Note: Make sure you have an empty _init_.py file in each directory.
Running test.py as main - Python 3.11 - Code:
import sys
import pathlib
directory = pathlib.Path(_file_)
parent_directory = directory.parent.parent.parent
sys.path.append(parent_directory)
from src.target_directory.target_module.py import FooBar
“Error No module named target_module”
Expected Behavior
I have run this code in Pycharm and it does not result in an error. It imports the module as expected.
Operating System
MacOS
Current Cursor Version (Menu → About Cursor → Copy)
Version: 0.43.6
VSCode Version: 1.93.1
Commit: a846435528b4b760494a836f96f0739889253530
Date: 2024-12-06T05:11:55.168Z
Electron: 30.5.1
Chromium: 124.0.6367.243
Node.js: 20.16.0
V8: 12.4.254.20-electron.0
OS: Darwin arm64 23.6.0
Additional Information
I need to run local tests before pushing my stack to AWS, this issue makes it convoluted to debug and test
Does this stop you from using Cursor
Sometimes - I can sometimes use Cursor
Hey, I don’t believe this to be a Cursor bug, but more a difference in how running the script in the terminal compares to running it in PyCharm.
PyCharm handles the working directory automatically, adding it to PYTHONPATH
, so imports resolve relative to the root and everything runs correctly.
However, when you run the script in the terminal (e.g., python src/tests/unit/test.py
), the working directory is set to the script’s location (src/tests/unit
), and Python only searches for modules relative to that.
Unless you explicitly add the project root to sys.path
or set PYTHONPATH
, Python can’t locate modules in parent directories, leading to the “No module named” error.
This can probably be fixed by running the script with PYTHONPATH
set to the project root or modifying sys.path
in the script.
Regardless, this isn’t a bug in the Cursor app, just something that PyCharm does for you that Cursor, being a universal editor and not just for Python, doesn’t do. You may be able to get an extension that emulates this behaviour in Cursor however.
Hi @danperks, thanks for the speedy response.
I might be misunderstanding your explanation but I tried running “sys.path.append(parent_directory)” in the script to make the parent directory available but it did not work.
The “parent_directory” value is the absolute path to the scr folder.
When I print out all values for sys.path, the correct path has been added but is not being used in the search for some reason.
I will try and pursue a workaround as being able to import parent functions for testing is critical to my Dev ops flow.
Unfortunately, this is more of a Python usage issue than a Cursor one. PyCharm is unique as it is a Python only editor, and therefore has some features out of the box that simplify running Python apps like this.
I’d recommend trying to print the sys.path in PyCharm and compare the difference, to see if there’s a discrepancy there.