Replies: 3 comments 5 replies
-
Could you properly format your post using code blocks for code and terminal outputs? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer! I think my code should be now better formatted I used this method because it’s what I typically use when working with standalone Python scripts outside a structured project. However, you’re right that relying on the project root or input file location might be better in Quarto. What I don’t understand is why this works in Jupyter but not when rendering with Quarto. Does Quarto’s rendering engine modify the environment so that Do you have any resources or insights on this difference? |
Beta Was this translation helpful? Give feedback.
-
Running into this issue as well. Desired code, using dynamic and robust filepath compilation that will work no matter which directory the file is run from, no matter which operating system is being used: import os
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "data")
#> "`NameError: name '__file__' is not defined`"
csv_filepath = os.path.join(DATA_DIR, "my_data.csv")
df.to_csv(csv_filepath, index=False) Runs into error. Alternative attempt, based on earlier comment in this thread proposing a reference to the root directory of the repo: DATA_DIR = "/docs/data"
#> "`OSError: Cannot save file into a non-existent directory: '/docs/data'`"
# even though that exists in the repo (its probably referencing the absolute system root filepath instead of the repo root directory).
csv_filepath = os.path.join(DATA_DIR, "my_data.csv")
df.to_csv(csv_filepath, index=False) Runs into error. Hard-coding the relative reference does work, but isn't an os agnostic solution, as windows slashes go the other way: DATA_DIR = "../../data"
csv_filepath = os.path.join(DATA_DIR, "my_data.csv")
df.to_csv(csv_filepath, index=False) |
Beta Was this translation helpful? Give feedback.
-
Description
Issue description
I can successfully run the code using the Jupyter extension in VS Code. However, when I attempt to render the document with Quarto, I encounter the following error:
NameError: name '__file__' is not defined
I have the following .qmd document:
Environment Details
quarto version
: 1.6.39OS
: ubuntuAdditional Context
__file__
works in regular Python scripts, but I suspect its behavior might differ in a Jupyter or Quarto context.__file__
in Quarto?Beta Was this translation helpful? Give feedback.
All reactions