RapydScript icon indicating copy to clipboard operation
RapydScript copied to clipboard

Cannot import from custom module in different directory

Open dewball345 opened this issue 3 years ago • 5 comments

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?

dewball345 avatar May 11 '21 20:05 dewball345

What does your compilation command look like?

valq7711 avatar May 11 '21 22:05 valq7711

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

valq7711 avatar May 11 '21 22:05 valq7711

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

dewball345 avatar May 12 '21 00:05 dewball345

ok, i solved it by configuring the --import-path attribute, though I don't understand the RAPYDSCRIPT_PATH variable

dewball345 avatar May 12 '21 02:05 dewball345

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.

Aareon avatar May 30 '21 11:05 Aareon