Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the path of the currently running script #194

Open
jmriego opened this issue Sep 3, 2020 · 9 comments
Open

Getting the path of the currently running script #194

jmriego opened this issue Sep 3, 2020 · 9 comments

Comments

@jmriego
Copy link

jmriego commented Sep 3, 2020

Hi! I have been a long time user and FreePIE and find it a really good program!

I have a script that is more complicated than usual and would like to separate it into a different library. At the moment, I'm doing this to be able to import libraries located in the FreePIE folder inside my home directory:
sys.path.append(os.path.expandvars(r'%USERPROFILE%\FreePIE'))

I would like instead to be able to replace the hardcoded directory to read from the same directory the currently executing script is located. I tried to use the __file__ python variable but it seems to not have what I expected. Is there any way to do this? Thanks!

@AndersMalmgren
Copy link
Owner

Hi the script is read into memory and executed so it does not have any connection the file when executed.

I think the default path is the lib path or similar. I haven't touch the python bits in years I'm afraid.

@jmriego
Copy link
Author

jmriego commented Sep 3, 2020

thanks for your answer! I checked the path and it seems like it's only reading from the pylib folder
do you think it would be possible to add the current file name script as a variable somewhere? If it wouldn't be a lot of work

@AndersMalmgren
Copy link
Owner

Problem is its the UI that loads the file and sends it off to the backend for exection. Needs some refactor for it to work. I might have time eventually, but if you want it done now you can branch freepie ;)

@AndersMalmgren
Copy link
Owner

If someone want a crack at this here is the place were the paths are beign loaded into the python egine

Engine.SetSearchPaths(GetPythonPaths());

@jmriego
Copy link
Author

jmriego commented Sep 4, 2020

thanks @AndersMalmgren ! that seems simple enough. I'll try and see if I can do this one

@cjroehrig
Copy link

cjroehrig commented Sep 4, 2020

I'm using a fairly complex script across several modules and I solved this by creating a windows symlink in the FreePIE/pylib directory that points to the root of my own modules dir under my Documents.
Something like:
mklink /d C:\Program Files (x86)\FreePIE\pylib\cjr C:\Users\cjr\Documents\CJRControllers\Lib\pylib
Then you can import modules by:
import cjr.mymodule

You'll also have trouble with file and line number errors which I work around by testing first under regular Windows Python to catch any syntax errors. (I check if sys.implementation.name != 'ironpython' and import a sim_plugin module that provides stubs for the FreePIE plugins I use).

You'll also notice that none of the special FreePIE globals are available in modules. I wrap them in a class object I call G which I pass to all the modules and save in their class instances (e.g. self.G.mouse.deltaX).

Also any "indexer" functions (filters) won't work inside a module. Easiest thing is to just not use them. They are easily implemented as dedicated variables/functions in your module's class and will be a bit faster too since they won't require a textual hash lookup, and it will also work with regular python.

@AndersMalmgren
Copy link
Owner

Maybe we should add a way to add extra paths using command line or config

@jmriego
Copy link
Author

jmriego commented Sep 5, 2020

thanks @cjroehrig ! That's a really good idea! I never thought of linking the directory itself, just files before

I have just sent a pull request related to this but it just adds the directory of the currently running file

@cjroehrig
Copy link

cjroehrig commented Sep 5, 2020

Maybe we should add a way to add extra paths using command line or config

That would be nice, but I think it should also include an update to error handling so that the syntax/runtime errors can report the file and line number. I had a go at a patch a couple years ago but I don't think I ever got it working. The filename doesn't seem to be readily accessible in the IronPython Exception which looks to be designed around streams. Needs more digging...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants