简体   繁体   English

如何通过绝对路径导入本地存储的模块?

[英]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.我有一个文件file.py ,我想导入一个本地模块。 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 .如果重要的话,服务器正在运行 Red Hat Linux 并且它没有pip

I tried mimic similar setup in my local.我尝试在本地模仿类似的设置。 I could able to get the result.我能够得到结果。

fileStack.py文件堆栈.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:设置:我从这里 git clone 熊猫源代码https://github.com/pandas-dev/pandas/tree/master/pandas我的目录结构如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM