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

Cant find module in CWD if module name is different from the filename #18630

Closed
TsurHerman opened this issue Sep 22, 2016 · 9 comments
Closed
Labels

Comments

@TsurHerman
Copy link

using julia 0.5rc4
LOAD_PATH contains current directory.
I have a filename AAA.jl defining en empty module BBB.jl

> #AAA.jl
> module BBB
> end

in a new shell

julia> using BBB
ERROR: ArgumentError: Module BBB not found in current path.
Run `Pkg.add("BBB")` to install the BBB package.
 in require(::Symbol) at .\loading.jl:365

Another peculiarity, if I try an import using the filename , then the second time it works:

julia> using BBB
ERROR: ArgumentError: Module BBB not found in current path.
Run `Pkg.add("BBB")` to install the BBB package.
 in require(::Symbol) at .\loading.jl:365

julia> using AAA
WARNING: requiring "AAA" in module "Main" did not define a corresponding module.

julia> AAA
ERROR: UndefVarError: AAA not defined

julia> using BBB

julia>
@stevengj
Copy link
Member

This is how using BBB works: if BBB was not already imported, then it looks for a file BBB.jl or BBB/BBB.jl in the path. How would you expect it to find BBB in a file AAA.jl automatically?

If you want to load BBB from the file AAA.jl, the right thing is:

include("AAA.jl") # execute everything in AAA.jl, which imports the BBB module
using BBB # import the exported symbols from the BBB module

@stevengj
Copy link
Member

using AAA imports the file AAA.jl, which defines the BBB module, but then fails because no AAA module was defined.

@yuyichao
Copy link
Contributor

You get BBB to because you are in Main. #17997

@TsurHerman
Copy link
Author

I suggest the following for better readability on the expense of maybe less usability :

include("A.jl") -> evaluate contents A.jl in the current scope
using A from B -> evaluates contents of module A from file B.jl in the current scope
import A from B -> evaluate contents of module A from file B.jl in current scope.A and export exported symbols to current scope

@stevengj
Copy link
Member

stevengj commented Sep 23, 2016

@TsurHerman, that would be quite a large departure from how Julia works. using A or import A currently includes and evaluates the whole A.jl file, not just the definition of module A (if any) in that file. There is no notion of evaluating the contents of one module extracted out of one file, and in fact trying to implement that would break the __precompile__(true) mechanism for specifying which modules are precompiled.

(Furthermore, your proposal swaps the meaning of import and using. In Julia using A is what exports symbols to the current scope.)

@TsurHerman
Copy link
Author

Thanks for clearing it out .. it should be stated somewhere very clearly that import and using first perform include modulename.jl.

There should be a way to work more naturally when developing.

I am currently using 'import' only on my code since I reload it often using a convenient macro @!
However I would like to be able to use 'using' without needing to kill and restart the session every code change I make.

So there must be a way to tag each symbol with the module it came from, and to further more automatically monitor the file system for changes and to reload modules and invalidate affected symbols.

This behavior can be only in interactive mode as probably it has some effect on performance.
This way the barrier for new users of the language will be greatly lowered , and you will have much less
repetitive explanations to make to people like me :-) .

So far my initial experience with julia is excellent.

@stevengj
Copy link
Member

@TsurHerman
Copy link
Author

I fumbled with that .. and didn't see any benefit over reloading my modules the normal way.
I think the syntax of that package was old and so it threw an error at me... as an exercise I tried to fix that package.

I really like Julia but the infrastructure is lacking and some of the available packages are still immature and not properly maintained.

A maintained small and simple package is much better than an obscure generic code wizardry.

@yuyichao
Copy link
Contributor

AFAICT there's nothing that needs to be done here. (Top level) Modules that are loaded automatically are always required to have the same module name and file (base)name. The confusion of getting a different module in Main is #17997. The syntax for relative loading/evaluation in a sub module is basically #4600

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

No branches or pull requests

4 participants