Python Forum
Setup working directory in IDLE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setup working directory in IDLE
#1
Hello,
Does exist a possibility to specify default working directory in IDLE tool under Ubuntu ?
Under working directory I mean the output of the os.getcwd() upon launching IDLE.
Thanks.
Reply
#2
I don't use IDLE except for very elementary code, it has issues you should look to use a different IDE, there are many available, and your choice will be made after trying a few out.
Many of the staff here use VSCode (not to be confused with Visual Studio). It's free, and easy to use.

I use the following statement at the start of each module in my project.
os.chdir(os.path.abspath(os.path.dirname(__file__)))
It sets the starting point for every node of my directory tree. (script directory as base.
once set, I then have a model that has the tree structure (relative to my src directory) for the entire projrct
which looks similar to:

PathTree.py
import os
from pathlib import Path


class PathTree:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        homepath = Path('.')
        rootpath = homepath / '..'

        self.docpath = rootpath / 'doc'
        self.docpath.mkdir(exist_ok=True)

        self.datapath = rootpath / 'data'
        self.datapath.mkdir(exist_ok=True)

        self.databasepath = self.datapath / 'database'
        self.databasepath.mkdir(exist_ok=True)

        self.excelpath = self.datapath / 'excel'
        self.excelpath.mkdir(exist_ok=True)

        self.csvpath = self.datapath / 'csv'
        self.csvpath.mkdir(exist_ok=True)

        self.htmlpath = self.datapath / 'html'
        self.htmlpath.mkdir(exist_ok=True)

        self.imagepath = self.datapath / 'images'
        self.imagepath.mkdir(exist_ok=True)
        
        self.jsonpath = self.datapath / 'json'
        self.jsonpath.mkdir(exist_ok=True)
        
        self.logpath = self.datapath / 'logs'
        self.logpath.mkdir(exist_ok=True)

        self.pandaspath = self.datapath / 'pandas'
        self.pandaspath.mkdir(exist_ok=True)

        self.prettypath = self.datapath / 'pretty'
        self.prettypath.mkdir(exist_ok=True)

        self.tmppath = self.datapath / 'tmp'
        self.tmppath.mkdir(exist_ok=True)

        # urls:
        self.AmericasStockExchanges = "https://en.wikipedia.org/wiki/List_of_stock_exchanges_in_the_Americas"
        self.ASE_savefile = self.htmlpath / "AmericasStockExchanges.html"


if __name__ == '__main__':
    PathTree()
to use, simply import the module and create an instance
example:
try_pathtree.py
from PathTree import PathTree
import requests


class MyClass:
    def __init__(self):
        self.ptree = PathTree()
    
    def Do_something(self):
        response = requests.get(self.ptree.AmericasStockExchanges)
        if response.status_code == 200:
            with self.ptree.ASE_savefile.open('wb') as fp:
                fp.write(response.content)
        else:
            print(f"Unable to load URL: {self.ptree.AmericasStockExchanges}")


def main():
    mc = MyClass()
    mc.Do_something()


if __name__ == '__main__':
    main()
so, the first time PathTree is imported, it will create the directory structure if it's not already there, or if new directories need to be created.

usage is simple, and when you add a new node (directory) to the PathTree, every module in your project will immediately have access to that node.
Reply
#3
Use python -m idlelib in folder you want to work from.

For VS Code i always use code . from command line,then VS Code start up in folder from where command is given from.
IDLE is not so good,and has mostly new users before the figurer that there is better alternatives.
I like ptpython for interactive stuff,and other one is IPython.
Reply
#4
Ok, thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 681 Nov-28-2023, 11:36 PM
Last Post: chitarup
  create a default path with idle to a specific directory greybill 0 846 Apr-23-2023, 04:32 AM
Last Post: greybill
  setup() from turtle module not working bobfat 7 5,992 Oct-28-2019, 11:05 AM
Last Post: newbieAuggie2019
  F-String not working when in IDLE editor nadimsarrouh 5 5,828 Jan-29-2019, 10:02 AM
Last Post: nadimsarrouh
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,540 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  setting working directory in pycharm saisankalpj 10 12,921 Dec-06-2018, 07:58 PM
Last Post: snippsat
  CWD (Current working directory) pcsailor 5 4,600 Nov-11-2018, 10:14 AM
Last Post: Larz60+
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 3,282 Sep-14-2018, 07:54 PM
Last Post: Larz60+
  how to save python file to working directory rpdohm 3 5,951 Sep-14-2017, 06:16 PM
Last Post: rpdohm
  Python IDLE 3.6.2 on WIn7 vs Pyhton 3 IDLE raspberry djdan_23 5 5,653 Sep-07-2017, 12:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020