User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Adding a path.

Tue Sep 13, 2016 5:41 pm

I have a script that needs access to a folder called Things in my working directory to load some modules. What I'm using is this...

Code: Select all

sys.path.append(os.getcwd() + "\Things")
... but it seems a bit clumsy using sys and os to do something so simple. What's the best way, bearing in mind it needs to be cross platform.

Dave.
Apple say... Monkey do !!

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Adding a path.

Tue Sep 13, 2016 6:13 pm

davef21370 wrote:I have a script that needs access to a folder called Things in my working directory to load some modules. What I'm using is this...

Code: Select all

sys.path.append(os.getcwd() + "\Things")
... but it seems a bit clumsy using sys and os to do something so simple. What's the best way, bearing in mind it needs to be cross platform.
Are you trying to build something that will look into the Things directory for subprograms to run in any current directory you happen to be running the program in or do you want it to go after your ~/Things directory specifically? I guess I was just wondering if you have a lot (or at least two) Things directories on your system. The usual name for such a directory is ~/bin

Even if you have a lot of Things directories, it might be more clear to build a variable that was the path to this specific directory and then add the specific program to that when running it. I'd also look into defining "Things" in a central location where you can easily change it without having to search your source file needlessly.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Adding a path.

Tue Sep 13, 2016 6:37 pm

The script needs to load a few "standard" classes then a load of custom ones which will very likely change between users so the "Things" folder containing the custom classes is there to keep things tidy.

Dave.
Apple say... Monkey do !!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Adding a path.

Tue Sep 13, 2016 7:47 pm

Apologies if I'm missing something. Is Things a subfolder of the folder in which your python script is located?

Why not just add an empty __init__.py file inside Things and then, in your code do:

Code: Select all

import Things.mymodule as mymodule
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Adding a path.

Tue Sep 13, 2016 8:30 pm

elParaguayo wrote:Apologies if I'm missing something. Is Things a subfolder of the folder in which your python script is located?

Why not just add an empty __init__.py file inside Things and then, in your code do:

Code: Select all

import Things.mymodule as mymodule
[/
elParaguayo wrote:Apologies if I'm missing something. Is Things a subfolder of the folder in which your python script is located?

Why not just add an empty __init__.py file inside Things and then, in your code do:

Code: Select all

import Things.mymodule as mymodule
That's an option but may complicate things. The code's a text based adventure framework aimed at getting kids coding by "filling in the blanks" at first then pushing them to create & change the properties and functions of objects in the game. Really need to keep it simple, can post more details over this weekend.

Regards.
Dave.
Apple say... Monkey do !!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Adding a path.

Wed Sep 14, 2016 5:51 pm

elParaguayo wrote:Apologies if I'm missing something. Is Things a subfolder of the folder in which your python script is located?

Why not just add an empty __init__.py file inside Things and then, in your code do:

Code: Select all

import Things.mymodule as mymodule
Tried that and it's a very nice solution. I've used...

Code: Select all

from Things import Cheese, Dog, Stone, etc, etc
which works a treat and keeps it simple. Thankyou.

Dave.
Apple say... Monkey do !!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Adding a path.

Wed Sep 14, 2016 9:18 pm

Great. Glad it helped.

Look forward to seeing your code.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”