最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Jupyter interactive window in VSCode attached to dev container is priting a wrong path for __file__ - Stack Overflow

programmeradmin11浏览0评论

I have an official Python docker container (python:3.11-slim-bookworm) running with a vanilla bind mount on my host, defined as follow in a Compose file:

services:
  app:
    ...
    volumes:
      - ./src:/app

I'm using "Dev Containers: Attach to Running Container..." from my 1st VSCode windows opened at the root of my project to actually "log into" the running container of my project (which has been started with docker compose up -d) in order to benefit from the correct version of Python that I need.

I also have both the Python and Jupyter extension installed inside the container through the 2nd VScode window that has been opened by the "Dev Containers: Attach to Running Container..."

Now, always in this 2nd VSCode window, from the Python interactive tab, I can see this strange behavior:

import os

# This first print is fine and reflecting the actual path inside the container:
print("Current working directory in Jupyter:", os.getcwd())
# Current working directory in Jupyter: /app

# This second print is getting me crazy because there is no such intermediary src/ folder
# inside the container:
print("My script folder path in Jupyter:", os.path.abspath(__file__))
# My script folder path in Jupyter: /app/src/mymodule/my_script.py

I also don't understand why __file__ is focused on my_script.py here, because I didn't run any cell from this file yet. Anyway...

My folder structure from inside the container is as follow:

user@app-container:/app$ tree -L 2
.
├── __init__.py
├── mymodule
│   ├── __init__.py
│   └── my_script.py
└── app.py

Can anybody explain why is VSCode seeing some /app/src/ folder inside the container?
The only place in my whole code base where the word "src" is, is in the volume section of my Compose file as described above and it's all about a path on my localhost, not inside the container.
Also, from the bash terminal inside VSCode 2nd window, there is no such /app/src/ folder, and if I try to os.chdir('/app/src/') in the interactive window, it obviously fails on a FileNotFoundError: [Errno 2] No such file or directory: '/app/src/'

Versioning

Python kernel in the container: 3.11.11
Docker: 27.5.1
VSCode: 1.96.4 with:

  • Jupyter extension: 2024.11.0
  • Python extension: 2024.22.2
  • Dev Containers: 0.394.0

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论