RapydScript
RapydScript copied to clipboard
Cannot import from custom module in different directory
Hello There,
This is my directory structure:
module1
---__init__.pyj
---test.pyj
file1
---__init__.pyj
---main.pyj
in test.pyj
def hello():
print("Hello")
and in my main.pyj
from module1.test import hello
However, when I compile my python, I get this error message:
[ImportError: Failed Import: 'module1' module doesn't exist in any of the import directories: ., C:\Users\*username*\rapydscript_ryact\RapydScript\src\lib] {
filename: ←[32m'main.pyj'←[39m,
readfile_error: ←[90mundefined←[39m
}
Why is this happening? And how can i fix it?
What does your compilation command look like?
from the doc:
RapydScript first tries to search the current directory of the file for the import. If that fails, it tries to search user-defined imports (which can be set via RAPYDSCRIPT_PATH environment variable as colon-separated directories, or semi-colon separated on Windows), and finally in the RapydScript builtin directory. This search order allows the user to easily override inbuilt functions.
You have two ways:
- define RAPYDSCRIPT_PATH
- create a simple one-line file with
import file1.main
in the parent folder of your packages and pass it to the compiler
ok, i'm kind of confused on the RAPYDSCRIPT_PATH one; Are you supposed to change the environment variable whenever you create a new package, or are editing in a new project? That would be a nightmare! Also, I tried the second method but it didn't work
ok, i solved it by configuring the --import-path attribute, though I don't understand the RAPYDSCRIPT_PATH variable
For example, if you wanted to import from a directory using --import-path
, you would do something like:
rapydscript main.py --import-path "./lib"
Using environment variables is a little different.
export RAPYDSCRIPT_PATH="./lib"; rapydscript main.py
If you wanted to save this environment variable permanently, add it to your .profile
. Your profile will depend on your OS.