Python Forum
"Automate the Boring Stuff with Python" creating a path works but only for CMD promt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Automate the Boring Stuff with Python" creating a path works but only for CMD promt
#1
I followed the instructions in that book step by step, but it didn't get me nowhere with adding custom paths.
The book says:
From System variables, select the Path variable and click Edit. In the Value text field, append a semicolon, type C:\MyPythonScripts, and then click OK. Now you can run any Python script in the C:\MyPythonScripts folder by simply pressing WIN-R and entering the script’s name.

Because that didn't work at all, I did this:
I did not use any semicolons(why would I?), just added C:\\MyPythonScripts as a custom path and it worked in CMD promt. I can now run a program there by just typing the name of the program, but it still doesn't work when using WIN-R. Why would that be?

Thanks, and sorry if I was unclear about anything.
Reply
#2
I have the same question. Huh I'll leave a comment here hoping somebody can provide an answer.
Reply
#3
here's what I do. I have one module for each project that lays out all of the directories, URL's and common file locations in a relative structure, using pathlib.
Here's a sample for a geocoding project:

the module is named GeoPaths.py and is imported by just about every other module in the project.
A neat feature of using something like this, is that you can run it on it's own in a copy of the project to
immediately set up your directory structure (it will create missing directories, but will leave already existing
directories alone):

GeoPaths.py
import os
from pathlib import Path


class GeoPaths:
    def __init__(self, depth=0):
        dir_depth = abs(depth)
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

        self.homepath = Path('.')

        while dir_depth:
            self.homepath = self.homepath / '..'
            dir_depth -= 1

        rootpath = self.homepath / '..'

        self.docspath = rootpath / 'docs'
        self.docspath.mkdir(exist_ok=True)

        self.testspath = rootpath / 'tests'
        self.testspath.mkdir(exist_ok=True)

        self.datapath = rootpath / 'data'
        self.datapath.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.jsonpath = self.datapath / 'json'
        self.jsonpath.mkdir(exist_ok=True)
        
        self.MasterAddressPath = self.datapath / 'MasterAddressDatabase'
        self.MasterAddressPath.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)

        # Osm data is arranged by state and file type.
        # A rather longdirectory tree, but laid out here for ease of use in software
        self.osmpath = self.datapath / 'osm'
        self.osmpath.mkdir(exist_ok=True)

        self.geofabrik_datapath = self.osmpath / 'GeofabrikAndCensus'
        self.geofabrik_datapath.mkdir(exist_ok=True)

        # URL's
        self.TigerLineGeoDatabase: 'https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-geodatabase-file.html'
        self.qgis_plugins = 'https://plugins.qgis.org/plugins/?page=1&&'
        self.osmfilelink = 'https://ftp.osuosl.org/pub/openstreetmap/planet/'
        self.geofabrikserver = 'https://download.geofabrik.de/north-america.html'

        # Common files:
        self.geofabrikjson = self.jsonpath / 'GeofabrikLinks.json'


if __name__ == '__main__':
    GeoPaths()
Before running the script, my directory structure for a new project looks like this:
Output:
├── src │   └── GeoPaths.py └── venv ...
After running GeoPaths.py directory structure looks like this:
$ . ./venv/bin/activate
(venv)$ python src/GeoPaths.py
Output:
. ├── data │   ├── csv │   ├── html │   ├── json │   ├── MasterAddressDatabase │   ├── osm │   │   └── GeofabrikAndCensus │   ├── pretty │   └── tmp ├── docs ├── src │   └── GeoPaths.py ├── tests └── venv ...
Now, assume you have a module named MyModule.py in the src diretory, and you want to open a json file named sillyfile.json.
here's the code that would do that:

MyModule.py
from GeoPaths import GeoPaths
import json


class MySillyClass:
    def __init__(self):
        self.gpaths = GeoPaths()
        self.jsonfile = self.gpaths.jsonpath / 'sillyfile.json'

    def create_dict(self):
        sillydict = {
            'Cowboys': '21',
            'GreenBayPackers': '7'
        }

        with self.jsonfile.open('w') as fp:
            json.dump(sillydict, fp)

    def read_it_back(self):
        with self.jsonfile.open() as fp:
            read_sillydict = json.load(fp)
        for key, value in read_sillydict.items():
            print(f"{key}: {value}")    

def main():
    mcc = MySillyClass()
    mcc.create_dict()
    mcc.read_it_back()


if __name__ == '__main__':
    main()
you can actually run this

results:
Output:
Cowboys: 21 GreenBayPackers: 7
The depth attribute in GeoPaths.py can be used when code in in a subdirectory of src.
Increment by on for each sublevel, and paths will automatically be adjusted for all source code in that subdirectory.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,151 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Python Regular expression, small sample works but not on file Acernz 5 2,861 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Run the code for some stuff it does not return me why Anldra12 3 2,798 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  Unable to print stuff from while loop Nick1507 4 2,280 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,133 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Works with Curl. Can't get it to work in Python bazcurtis 3 2,499 May-07-2020, 07:47 AM
Last Post: bazcurtis
  Learning to have Class and doing stuff with it... bako 2 1,958 Apr-29-2020, 05:07 PM
Last Post: bako
  New to Python, How does this lambda expression works? Joshh_33 2 1,998 Mar-26-2020, 03:32 PM
Last Post: Joshh_33
  How to prevent Command Promt from closing when I turn it off in Python? binhduonggttn 1 1,959 Mar-06-2020, 10:12 AM
Last Post: ibreeden
  time.sleep works erratically, a bug in Python? stipcevic 2 3,819 Jan-21-2020, 09:38 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

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