Python Forum
pdf2image, poppler and paths
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pdf2image, poppler and paths
#11
(Jun-12-2022, 01:56 PM)Larz60+ Wrote: Note: You can eliminate steps 7 and 8 as this will happen the first time MyPaths in imported into any of your scripts. I haven't tested outside of virtual environment, as, frankly, I always use a virtual environment ofr any new project, but this should work without.

Thanks, and I understand that if the class 'MyPaths' is included in a Python script it will create the paths if not already present. But how do I use the class in another scripts, without having to have all the code for the class ?

For example, in PHP, it is simply an "include" statement.
Reply
#12
MyPaths.py should be put into a separate module, only one time. It is expected to be imported in any module that needs access to the path structure.

I show it as a separate module in post 6 (first one) https://python-forum.io/thread-37451-pos...#pid158233.
That code should be saved once as MyPaths.py. (rename, if you wish, filename and class name should be the same then import name needs to be the same as the new class name).

Try the example in post 10. It shows exactly how to do this. The MyPaths.py module only has to be coded once, same with the virtual environment, once per project no mater how many modules it contains.

There is a tutorial that i wrote, not for this purpose, see SqlPaths.py, and note that it's included in each module.
https://python-forum.io/thread-24127.html

One great side effect of keeping paths separate is that a path can be changed and all other code will immediately use the new path.
jehoshua likes this post
Reply
#13
(Jun-13-2022, 07:17 AM)Larz60+ Wrote: MyPaths.py should be put into a separate module, only one time. It is expected to be imported in any module that needs access to the path structure. {snip}

Thanks, I missed seeing

from MyPaths import MyPaths
and

mpath = MyPaths()
in the post at https://python-forum.io/thread-37451-pos...#pid158233

It works fine, thanks. :)
Reply
#14
There's also a modification that can be applied that allows you use of this class when your source code spans multiple directories. If you have a need something like this, It's a simple modification and works well, let me know.
jehoshua likes this post
Reply
#15
(Jun-13-2022, 11:04 AM)Larz60+ Wrote: There's also a modification that can be applied that allows you use of this class when your source code spans multiple directories. If you have a need something like this, It's a simple modification and works well, let me know.

Yes thanks, if you can share that.
Reply
#16
Warning: This is a hack
There's probably a better way to do this, but I've used it for years and it suits my purposes:
Also, probably won't work on MS Windows since I use symbolic links. Not sure, I'm pretty much
A Linux only person.

import os
from pathlib import Path
 
 
class MyPaths:
    def __init__(self, depth=0):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

        dir_depth = abs(depth)

        HomePath = Path(".")

        while dir_depth:
            HomePath = HomePath / ".."
            dir_depth -= 1
 
        rootpath = HomePath / ".."
 
        self.datapath = rootpath / "data"
        self.datapath.mkdir(exist_ok=True)
 
        self.csvpath = self.datapath / "csv"
        self.csvpath.mkdir(exist_ok=True)
 
        self.docpath = rootpath / "docs"
        self.docpath.mkdir(exist_ok=True)
 
        self.jsonpath = self.datapath / "json"
        self.jsonpath.mkdir(exist_ok=True)
 
        self.htmlpath = self.datapath / "html"
        self.htmlpath.mkdir(exist_ok=True)
 
        self.pdfpath = self.datapath / 'PDF'
        self.pdfpath.mkdir(exist_ok=True)
 
        self.tmppath = self.datapath / "tmp"
        self.tmppath.mkdir(exist_ok=True)
 
 
if __name__ == "__main__":
    MyPaths(depth=0)
now if you have a source directory like:

├── data
│ ├── csv
│ ├── PDF
│ └── tmp
├── docs
├── src
│ ├── ProvingGround
│ └──
│ ├─ MyProg.py
│ └── Create symbolic link to MyPaths.py here
└── venv
└── ...

then to use MyPaths in MyProg.py, instanceiate like mpath = MyPaths(depth=1)
if you have more directories below Proving Ground, increase size of depth accordingly

EDIT:
Note You most likely figured this out, depth is optional, 0 is assumed if not supplied.
jehoshua likes this post
Reply
#17
(Jun-13-2022, 10:42 PM)Larz60+ Wrote: Warning: This is a hack
There's probably a better way to do this, but I've used it for years and it suits my purposes:
Also, probably won't work on MS Windows since I use symbolic links. Not sure, I'm pretty much
A Linux only person.

Great, thanks very much for your help. :)
Reply
#18
jehoshua Wrote:But how do I use the class in another scripts, without having to have all the code for the class ?

For example, in PHP, it is simply an "include" statement.
You should look at how packages works.
Here a post with some examples that i have made.
So you can make something homemade that work for you like Larz60+ do,if other shall use you package maybe need an other approach.

My goal if make a package is to make import statement simple and work on all OS if other shall use my package.
As a example look at Requests on simple import import requests.
Here the most useful module are lifted up them import is simple as this.
>>> import requests
>>> 
>>> requests.get
<function get at 0x000002AA430D5B40>
>>> requests.post
<function post at 0x000002AA430D5CF0>
>>> requests.codes
<lookup 'status_codes'>
>>> requests.status_codes
<module 'requests.status_codes' from 'C:\\Python310...'>
>>> requests.head
<function head at 0x000002AA430D5C60>
jehoshua likes this post
Reply
#19
(Jun-14-2022, 12:46 AM)snippsat Wrote: You should look at how packages works.
Here a post with some examples that i have made.
So you can make something homemade that work for you like Larz60+ do,if other shall use you package maybe need an other approach. {snip}

Great, thank you. Some interesting articles and examples to digest.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Windows paths issue otalado 3 1,471 May-29-2022, 09:11 AM
Last Post: snippsat
  automatically get absolute paths oclmedyb 1 2,127 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  chkFile with absolute paths JarredAwesome 7 3,012 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  Paths millpond 12 5,226 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Problems with windows paths delphinis 6 5,203 Jul-21-2020, 06:11 PM
Last Post: Gribouillis
  'No module named pdf2image' ironfelix717 13 22,275 Jul-24-2019, 11:54 AM
Last Post: snippsat
  Shortest paths to win snake and ladder sandaab 5 4,258 Jun-30-2019, 03:20 PM
Last Post: sandaab
  How to handle paths with spaces in the name? zBernie 1 6,745 Nov-22-2018, 04:04 AM
Last Post: ichabod801
  Question: Paths and writing to a file mwmaw 6 6,503 Dec-20-2016, 03:44 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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