Julia语言中正确引入文件并使更新生效的方法
Hey there! I totally get where you're coming from—Julia's module system works a bit differently than Matlab's, but you can absolutely replicate that workflow where modifying a function file and rerunning your main script picks up the changes automatically. Let's break this down step by step.
Basic Setup (No Modules)
First, let's mirror your Matlab file structure:
- Create a
srcfolder with two files:main.jlandanotherfunc.jl
anotherfunc.jl
function anotherfunc(x) println(sin(x)) # Matlab's disp() translates to println() in Julia end
main.jl
# Load the function file directly include("anotherfunc.jl") function main() anotherfunc(0) end # Execute the main function main()
How to Run
Open a Julia REPL, navigate to your src folder, and run:
include("main.jl")
You'll see 0.0 (Julia returns floating-point values for trig functions, unlike Matlab's integer 0 here).
Updating the Function
Now edit anotherfunc.jl to use cosine instead:
function anotherfunc(x) println(cos(x)) end
Just re-run include("main.jl") in the REPL—you'll get 1.0 immediately. No extra steps are needed here since we're working in the global scope, where redefining functions overwrites the old version.
Better Practice: Using Modules + Revise.jl
For larger projects, using modules is more organized, but Julia caches loaded modules by default. To get that seamless Matlab-style auto-reload, we'll use Revise.jl—it's a must-have tool for Julia development!
Step 1: Install Revise
Open the Julia REPL, type ] to enter package mode, then run:
add Revise
Step 2: Update Your Files
anotherfunc.jl (now a module)
module MyFuncs # Export the function so we can use it directly in main.jl export anotherfunc function anotherfunc(x) println(sin(x)) end end # end module
main.jl (integrates Revise)
# Load Revise first—it needs to initialize before your modules using Revise # Include and activate your module include("anotherfunc.jl") using .MyFuncs function main() anotherfunc(0) end main()
Step 3: Run and Update
- Run
include("main.jl")in the REPL—you'll get0.0as before. - Now edit
anotherfunc.jl(e.g., switch tocos(x)), then either re-runmain()directly in the REPL or re-includemain.jl. Revise will automatically detect the changes and load the updated function, giving you1.0without needing to restart Julia.
Bonus: Renaming Functions
If you want to rename the function (like your example where you switched to otherfunc), just update the module:
module MyFuncs export otherfunc # Update the export line to match the new function name function otherfunc(x) println(cos(x)) end end # end module
Then in main.jl, update the call to otherfunc(0)—Revise will pick up the new function name automatically when you rerun.
Key Notes
- Global scope vs modules: The basic
includemethod works for small scripts, but modules keep your code clean and avoid name collisions. - Revise is your friend: It eliminates the need to restart Julia every time you change code, making your workflow feel identical to Matlab's.
内容的提问来源于stack exchange,提问作者Thomas

