Troubles with loading data when running from my IDE

Why icons, images, and databases fail to load when you run your PyQt app from VS Code
Heads up! You've already completed this tutorial.

Icons, pictures, and databases don't load when I run my app in VS Code, but everything works fine from the command prompt. Why does this happen, and how can I fix it?

This is one of the most common stumbling blocks when developing Python GUI applications. You set up your icons, images, or database files, everything runs perfectly from the terminal, and then you hit "Run" in VS Code (or another IDE) and... nothing loads. No errors, just missing resources.

The good news is that this has a straightforward explanation and a reliable fix.

The problem: working directories

When you run a Python script, your program has a working directory — the folder that Python treats as "here" when resolving relative file paths. If your code loads an image like this:

python
icon = QIcon("icons/app.png")

Python will look for icons/app.png relative to the current working directory, not relative to the script file itself.

When you open a terminal, cd into your project folder, and run python my_app.py, the working directory is your project folder. The relative path icons/app.png points to the right place, and everything works.

But when you run from an IDE like VS Code, the working directory might be set to something else entirely — the workspace root, your home folder, or some other default. Your relative paths now point to the wrong location, and Python silently fails to find the files.

This affects everything that uses a file path: icons, images, stylesheets loaded from files, SQLite databases, MySQL connection config files — anything where you reference a file by a relative path.

Confirming the issue

You can verify this by printing the current working directory at the top of your script:

python
import os
print("Working directory:", os.getcwd())

Run this from the terminal and from your IDE. If the paths are different, you've found the cause.

The fix: build paths relative to your script

The most reliable solution is to construct file paths relative to the location of your Python script, rather than relying on the working directory. Python makes this easy with os.path or pathlib.

Here's the pattern using os.path:

python
import os

# Get the directory where this script lives
basedir = os.path.dirname(__file__)

__file__ is a special Python variable that holds the path to the current script. By extracting its directory with os.path.dirname(), you get a stable reference point that doesn't change depending on where you run the script from.

Now, whenever you need to load a file, use os.path.join() to build the full path:

python
icon_path = os.path.join(basedir, "icons", "app.png")
icon = QIcon(icon_path)

For a database file:

python
db_path = os.path.join(basedir, "data", "mydb.sqlite")

This works the same way whether you run from the terminal, VS Code, PyCharm, or any other environment.

A complete working example

Here's a small PyQt6 application that loads an icon and an image using this approach. You can adapt it to your own project structure.

python
import os
import sys

from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PyQt6.QtGui import QIcon, QPixmap

basedir = os.path.dirname(__file__)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("File Loading Example")

        # Load the window icon relative to the script location
        icon_path = os.path.join(basedir, "icons", "app.png")
        self.setWindowIcon(QIcon(icon_path))

        # Load an image into a label
        image_path = os.path.join(basedir, "images", "photo.png")
        pixmap = QPixmap(image_path)

        layout = QVBoxLayout()

        image_label = QLabel()
        image_label.setPixmap(pixmap)
        layout.addWidget(image_label)

        info_label = QLabel(f"Base directory: {basedir}")
        layout.addWidget(info_label)

        self.setLayout(layout)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

To use this example, create an icons folder with an app.png file and an images folder with a photo.png file next to your script. The paths will resolve correctly regardless of how you launch the program.

What about pathlib?

If you prefer the more modern pathlib module, the same approach works:

python
from pathlib import Path

basedir = Path(__file__).resolve().parent

icon_path = basedir / "icons" / "app.png"

The / operator on Path objects joins path segments together, which many people find more readable than os.path.join(). Either approach is fine — pick whichever you're more comfortable with.

Do you need to switch IDEs?

No. This is not a VS Code bug, and switching to a different IDE won't reliably solve the problem. Every IDE can potentially set a different working directory. The real fix is to make your code independent of the working directory by building paths relative to __file__, as shown above.

Once you adopt this habit, your applications will work consistently no matter where or how they're launched — from the terminal, from VS Code, from PyCharm, or by double-clicking the script in a file manager.

Summary

Relative file paths in Python are resolved against the current working directory, which varies depending on how you launch your script. IDEs like VS Code often set this to something other than your script's folder, causing icons, images, and data files to silently fail to load.

The fix is to always build file paths relative to __file__ using os.path.join() or pathlib.Path. This gives you consistent, predictable behavior everywhere. For more on working with images in your PyQt6 applications, see our guide on adding images to PyQt6 applications.

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick

(PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!

More info Get the book

Martin Fitzpatrick

Troubles with loading data when running from my IDE was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.