Python Forum

Full Version: Appending sys.path not working with spyder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
After many years of using VSCode, I am experimenting with using Spyder as my IDE, fed up with VSCode telemetry which occurs even though 'turned off'.

OS: Linux Mint 21.3

The following code using VSCode has always found the included module CCpaths
It also works if I execute from command line.

However this does not work with spyder, which gives me a 'module not found' error, and seems to ignore sys.path

The CommandProcessor
my path is as follows:

CommandProcessorProject
.
├── data
├── doc
├── requirements.txt
├── src
│     ├── common
│     │     ├── CCpaths.py
│     │     ├── images
│     │     │     ├── ...
│     │     ├── __init__.py
│     │── core
│     │    ├── CommandProcessor.py
│     │    └── __init__.py
├── venv
├── ...
import sys
sys.path.append("./src/common")

from CCpaths import CCpaths


class CommandProcessor:
    def __init__(self):
        self.cpath = CCpaths(depth=1)
        ...
I am stumbling through the setup of Spyder, and doing OK, but this one is frustrating me.
If this sys.path update produces no effect, it could mean that the current directory at run time is not the one containing src. Assuming that this code is in CommandProcessor.py, you could try
from pathlib import Path
here = Path(__file__).parent  # this is the 'core' directory
sys.path.append(str(here.parent/'common'))
Quote:here = Path(__file__).parent # this is the 'core' directory
That's too much common sense
When frustrated, I don't have any common sense.
I'll give it a go.

--- EDIT 5:01 A.M. EDT ---
Gribouillis ... That worked like a champ
What I had been using (for years) has always worked before, but never tested with other IDE's
Your method is more sure, and should work for most (if not all) environments.
Thank You