I'm having trouble re-using macros within a crate.
If a macro is defined in ./src/macros.rs
:
#[macro_export]
macro_rules! my_macro {
...
}
and used in ./src/lib.rs
:
#[macro_use]
pub mod macros;
I can't see this macro in ./src/submod/lib.rs
:
my_macro!(...);
It yields the error message error: macro undefined: 'my_macro!'
.
Is there a way I can import this macro in this child module submod
?
As we have discussed earlier it is not possible to import a module from the parent directory, so this leads to an error something like this. File “C:/Users/sai mohan pulamolu/Desktop/parentdirectory/subdirectory/temp.py”, line 2, in <module> In order to import a module, the directory having that module must be present on PYTHONPATH.
In order to import a module, the directory having that module must be present on PYTHONPATH. It is an environment variable that contains the list of packages that will be loaded by Python. The list of packages presents in PYTHONPATH is also present in sys.path, so will add the parent directory path to the sys.path.
You can use OS depending path in "module search path" which is listed in sys.path . So you can easily add parent directory like following import sys sys.path.insert (0,'..') sys.path.insert (0,'../..') This works both in python 2 and 3. Show activity on this post. Don't know much about python 2. import sys sys.path.append ('..')
When you are ready to import it to another workbook, just right-click on the workbook you want to place it in and choose Import File…. Importing code modules or macros is as easy as that! You can use any of these options to copy code to your personal macro workbook as well.
I figured it out! It is imported automatically, but I didn't realize that macros are imported in order!
I imported the submod
module before macros
, so my_macro
wasn't visible yet. By swapping the order, I got the expected behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With