Search code examples
pythonatom-editorworking-directory

How can I get Atom's script package to run a script from the script's current working directory?


For background, I am using the Atom editor with the script package installed.

Within Atom, there are two projects that I normally use.

D:/Data_files/Git/Projects
D:/Data_files/Python/Notes

When I normally open atom from the terminal, I have both projects open, and for whatever reason it has the default project or something set to the Git/Projects project

The Git/Projects directory is not important, but here is what the Python directory looks like.

D:/Data_files/Python/Notes/
    __init__.py
    files_and_exceptions.py
    text.txt

Here's what text.txt contains

This is the text of text.txt

Here is what files_and_exceptions.py contains

import os
print(os.getcwd)
os.chdir("../../Python/Notes")
print(os.getcwd)

with open('text.txt') as hello:

    contents = hello.read()
    print(contents)

Which outputs when I run it using CTRL+SHIFT+B from the files_and_exceptions.py using script

D:/Data_files/Git/Projects
D:/Data_files/Python/Notes
This is the text of text.txt

So even though I'm running it from the Python/Notes directory, the current working directory is set to Git/Projects. I want files_and_exceptions.py to print text.txt without importing and changing directories when running from the file because that's how it would work when I run it normally using Python's Shell. I know I can set it by opening Atom from the directories in separate windows but that gets irritating when I'm working with multiple projects at once. Is there any way to easily change the current working directory of Atom when working between projects or modify the script package to change the current working directory to the directory of the open tab (ie: files_and_exceptions.py) when I run it with CTRL+SHIFT+B?


Solution

  • In the default settings, script uses the project directory of the first project in your Project tree. In the settings of script, you can change the that the project directory of your current script (or the directory your script resides in itself).

    You go to settings (press Ctrl+,), then the Packages tab, you search for the script package and the first dropdown menu is that setting.


    If you don't want to change this, you can also have script run with a specific profile.

    In the command palette Ctrl+Shift+p, you search for script: Run options and you get a menu with some settings to start your script with. You can save those as profile and later use script: Run with Profile to reload.