Importing local module

Hello,

I am trying to run a Python script that uses the import(name) function to import a module from my local directory but it does not seem to work on SWAN. It works if I run it locally. Any ideas? Thank you!

Oskar

Hello Oskar,

Lets try with this and let me know.

in

export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.5/site-packages:$PYTHONPATH

put the right python version according to the stack you are using.
Cheers
Omar.

Hello Omar,

Thanks for the answer!

The module being imported in the script is a custom one just containing some functions someone wrote (so it is not installable with pip). Is it possible to import custom modules from the local directory? Thanks!

Cheers,
Oskar

Hello Oskar,

I did this small example for you and I tested it in SWAN

Maybe you are missing the __init__.py file.

Lets try and let me know.

Cheers
Omar.

Hi Omar,

I was missing the __init__.py file but it also did not work until I put the modules in a sub directory like you did (./mymodule). Thanks for that!

Cheers,
Oskar

1 Like

Hi again Omar,

Sorry for asking even more specific questions but it seems that I am still having some issues that I can’t seem to figure out. I am trying to run a piece of code that I did not write myself and it is importing modules in a way that I’m not able to solve using the method you just showed me. Would you mind taking a look?

I am trying to run erratics.py but the imports do not seem to work as they should. Thanks a lot for any input you can give…

Hello Oskar,

I got this error,

ModuleNotFoundError: No module named 'erratics'

erratics can not be imported because it is a notebook not a python module.
there is not file erratics.py to be imported.

Cheers
Omar.

Hi Omar,

Yes I realize the problem has to do with this - my first thought was to create a python module erratics.py that is just a copy of the code in erratics.ipynb, but this did not seem to fix the issue either. Do you think the problem is fixable or should I just try to rewrite the code? Thanks a lot.

Cheers,
Oskar

HI Oskar,

I did this corrections for you

erratics.py should not call loadFramework('erratics', datetime(2017, 1, 1, 0, 0, 0), datetime(2017, 1, 10, 0, 0, 0))
because it is recursive, due that loadFramework is importing again erratics.py

Cheers
Omar.

Hi Omar,

That makes sense now. Thanks a lot for the help, really appreciate it.

Cheers,
Oskar

1 Like