gator
gator

Reputation: 3523

How can I import a locally stored module via absolute path?

I have a file file.py which I want to import a local module. A server I do some work on doesn't allow me to install modules so I am trying to include them in my projects locally to reference. I have downloaded the necessary modules but I'm unable to install them.

#!/usr/bin/env python3

from sys import exit

try:
    import "/modules/pandas"
    import "/modules/numpy"
except ImportError:
    print("Modules not found!")
    exit(1)

if __name__ == '__main__':
    print("Modules loaded properly.")
    exit(0)

Above is the general idea of what I'm trying to do. My directory structure is like so:

parent/
-- file.py
-- modules/
-- -- pandas/
-- -- -- ...
-- -- -- __init__.py
-- -- -- ...
-- -- numpy/
-- -- -- ...
-- -- -- __init__.py
-- -- -- ...

How can I access these modules in this situation?

If it matters, the server is running Red Hat Linux and it does not have pip.

Upvotes: 0

Views: 46

Answers (1)

Ratnesh
Ratnesh

Reputation: 1700

I tried mimic similar setup in my local. I could able to get the result.

fileStack.py

import sys
try:
    import modules.pandas
    print("done!!")

except ImportError:
    print("Modules not found!")
    sys.exit(1)
if __name__ == '__main__':
    print("Modules loaded properly.")
    sys.exit(0)

Output:

done!!
Modules loaded properly.

Setup: I have git clone the pandas source code from here https://github.com/pandas-dev/pandas/tree/master/pandas My directory structure looks like this:

parent:
-- fileStack.py
-- modules
-- -- pandas
-- -- -- .github
-- -- --  pandas
-- -- -- -- __init__.py

Upvotes: 1

Related Questions