Module search path for importable modules

63 views
Skip to first unread message

John P Charlesworth

unread,
Oct 24, 2015, 7:14:02 AM10/24/15
to RapydScript
Now that we have Python-like imports,  I am wondering how to structure my code to allow importable modules to be shared between different applications. Essentially this comes down to the question of how RapydScript finds modules mentioned in import statements. 

I now have something approaching 20 applications, all of which share some code which could be migrated to an importable module. There is a single directory enclosing the whole project, in which each application has its own directory one level down from the project directory. The applications share javascript files which live in subdirectories of a javascript directory which is also one level down from the project directory. That presents no problems because the javascript files are loaded by script tags which have appropriate src attributes.

With this kind of structure, the logical place for Rapydscript files which might be imported by many applications would be another directory named, say, rslib, one level down from the project.

In Python, this could be made to work by including the path to rslib in the PYTHONPATH environment variable, or by modifying sys.path in the application code. Does Rapydscript have any equivalent way of telling the compiler where to look for module code which is located outside the branch of the directory tree that contains the importing application? 

John P Charlesworth

unread,
Nov 30, 2015, 8:13:39 AM11/30/15
to RapydScript
Since I have had no reply to this post, I presume that either there is no interest or no obvious answer. I will perhaps post a Feature Request on Issues to have a compile flag which can supply a path to be searched for imports.

Alexander Tsepkov

unread,
Nov 30, 2015, 8:37:22 AM11/30/15
to John P Charlesworth, RapydScript
Apologies, the email fell through the cracks. What's the context you're using this in? Browser? Node?

If it's the browser, simply including your shared libraries as a separate (exposed or bare) <script> tag should would
If it's node, do you have the libraries scattered all over the harddrive? Wouldn't this still have been an issue with the older import method?

There are possible alternatives for dealing with this in node world, but none appeal to me since now we're pretending not just that the syntax is Python-like but that the entire mechanism compile-time mechanism is like Python, which is akin to just wanting C-like syntax at first and then ending up pulling in the concept of Makefiles as well. I'd much rather handle this similarly to how node handles it (NODE_PATH?) rather than Python if this is specific to node environment. Would that work for you? If I'm not understanding something, please describe your situation in more detail.

John P Charlesworth

unread,
Nov 30, 2015, 1:17:49 PM11/30/15
to RapydScript, phil.cha...@physics.org
Perhaps the best way to describe it is to show a rough sketch of the directory structure.

project
         |---- app1
         |
         |---- app2
         |
         |---- app3
         |
         |---- javascript-libaries
         |
         |---- rapydscript-imports

The app directories house the RapydScript code of the main application file, the resultant Javascript, ML code for the HTML file and the resultant HTML file.

The javascript libraries directory houses RapydSript code and resultant Javascript code for separately compiled library modules which are pulled in by script tags in the HTML file.
           
The  rapydsscript-imports directory is a place where I want to house uncompiled Rapydscript code for importable modules which can be used in all the applications and are, in effect, pulled in by the Rapydscript compiler.

At the moment I have only two or three small importable RapydScript modules, used in only one application, so they currently reside in the relevant app directory However, I would like to import them into the other application as well but do not really want to duplicate them in each app directory. Nor do I want to put them in the rapyscript/src/lib directory of my RapydScript installation. I also envisage making more use of importable modules in the future. So what I'm looking for is some way to give the RapydScript compiler a path to be searched for the relevant .pyj files when it comes across an import statement.

I did have a look at the compiler code but concluded that rapydscript/src/lib was the only place searched for imports. My suggestion of a compile flag seemed a possibly easy way to give the compiler an additional search path but there are obviously other ways it could be done, for instance via an environment variable, which is one of the ways which Python uses for a similar purpose.

I hope that makes the purpose of the post clear. It is not a desperately urgent problem but a suggestion for something to be thought about for the future.   

Alexander Tsepkov

unread,
Nov 30, 2015, 1:25:24 PM11/30/15
to John P Charlesworth, RapydScript
RapydScript first uses local directory, then falls back to src/lib. Indeed, RapydScript's own src/lib would not be the right place for these libraries if they're part of your project. If I add a compile flag of --lib-dir or similar for a directory that would be searched after local but before src/lib in RS installation, would that satisfy your use case?
Reply all
Reply to author
Forward
0 new messages