How to Convert string variable to Raw literal path in Windows

Background

Hello,
I’m compiling Jython (Python2) on Windows.

I want to convert a string variable to a raw string because I want to make a path for a photo or self-written module.

Sample code

This cord make a path and no problem with import self wrote module

I can make a path if I directly write r to a string , but I want to do this for a variable.

import sys
from sys import path
from java.lang.System import getProperty

sys.path.append(r"C:\Users\For  Programming\fiji-win64\Fiji.app\ReserchScript")

import GrainAnalyzer # self-written Module

This cord dose not work_1

import sys
from sys import path
from java.lang.System import getProperty

modulepath1 = ("C:\Users\For  Programming\fiji-win64\Fiji.app\ReserchScript")
sys.path.append(modulepath1)

import GrainAnalyzer # self-written Module
#> No module named GrainAnalyzer

This cord dose not work_2

import sys
from sys import path
from java.lang.System import getProperty

modulepath1 = ("C:\Users\For  Programming\fiji-win64\Fiji.app\ReserchScript")
modulepath1 = repr(modulepath1)
sys.path.append(modulepath1)

import GrainAnalyzer # self-written Module
#> No module named GrainAnalyzer

If my English is difficult to understand , please tell me that. i rewrite about this.
Apologies in advance if this is covered elsewhere, can’t find it easily. Thanks.

Can you replace \ by / ? That should work on all operating systems…

1 Like

Do you mean you can’t do
modulepath1 = r"C:\Users.....\" so putting the r before the string when you declare modulepath1 (I am not sure you need the brackets by the way).
Or you get this path via a GUI and above is just an example ?

Actually you dont need to manually add to sys.path.
You can put your file GrainAnalyzer.py in Fiji.app/jars/Lib (create the Lib directory if not existing).
Then import GrainAnalyzer should work in the Fiji script editor in jython, without any more precaution.
I would recommend this solution, as it’s the most flexible if you need to use your stuff on another machine, compared to the hard coded path.

When you import it, Fiji compiles it to a .py$class object, which is not recompiled if you change GrainAnalyzer.py. So every time you change GrainAnalyzer.py, delete the corresponding class file, and potentially restart Fiji.

Also if you put your modules in a subdirectory of jars/Lib, make sure to put an __init__.py file (even empty) to make sure the directory is recognized as a package.

Good Morning.

I didn’t explain it well enough.
When I get the path(string) from GUI(e.g. # @File(label = "PluginPlace" , style="directory") modulepath1) , I want to get the path as raw strings to make a path certainly.

In our team, even if the same windows user compiles the same code, there is who can make a path and not make a path so I want to get the path variable as a raw string.

Thanks, Dan

Good Morning.

To remove Machine dependency , It would be a good idea to put self-written modules in the “Lib” folder from the beginning.

Thanks , Dan