如何包括Python中不同目录的其他功能的其他文件?

发布于 2025-01-21 19:28:08 字数 532 浏览 0 评论 0原文

我想将所有文件包括在my_lib中,因为该目录将包含每个库中使用的大多数功能。在这种情况下,我想导入my_pandas.py

我已经添加了包含__ INT __. pymy_pandas.py__ init的库目录my_lib

但是我无法运行以下命令: from my_lib import my_pandas

How can i run from my_lib import my_pandas command?

I want to include all files in my_lib as the directory will contains most used function per library. In this case i want to import my_pandas.py.

I already add the library directory my_lib which contains __init__.py and my_pandas.py.

However i cannot run the following command:
from my_lib import my_pandas

How can i run from my_lib import my_pandas command?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-01-28 19:28:09

不幸的是,在Python中找到自己的自定义模块。

通过添加__ Init __. Py,您将正确制成的my_lib进入模块中。有两种方法可以使模块正确导入,但是它们归结为同一件事:该模块需要在路径中的目录中以子目录的形式出现。

因此,您的解决方案是:

  1. 导航到/home/inetvmart/python_apps,然后在此处运行笔记本而不是内部/home/inetvmart/inetvmart/python_apps/my_lib(始终包含当前目录在路径中,my_lib在这种情况下将是当前目录内的子目录)
  2. add /home/home/inetvmart/inetvmart/python_apps 而不是 /home/inetvmart/python_apps/my_lib到您的路径(然后,父目录将在路径中。因此,您的自定义模块将

在此特定情况下是您的目录中的子目录),然后,您可能只能将第二个单元格更改为:sys.path.append(“/home/inetvmart/python_apps”)


我必须警告您,这是一个有点... hacky ... (修改Python脚本中的路径以确保您的导入成功)。

在中期,您可能想执行以下操作:

  • 创建一个称为eg /home/<您的用户名>/my_python_modules
  • 将此路径放入$ pythonpath环境环境中的 文件夹。变量(您可以在.bashrc/.zshrc/其他文件中设置)

,然后您可以在my_python_modules中导入自定义模块从机器上的任何地方。

Finding your own custom modules in Python is unfortunately a little fiddly.

You have correctly made my_lib into a module by adding __init__.py. There are two ways to get the module to import correctly, but they boil down to the same thing: the module needs to appear as a subdirectory inside a directory that is in your path.

Therefore your solutions are:

  1. navigate to /home/inetvmart/python_apps and run your notebook there instead of inside /home/inetvmart/python_apps/my_lib (the current directory is always included in the path, and my_lib will be a subdirectory inside your current directory in this case)
  2. add /home/inetvmart/python_apps rather than /home/inetvmart/python_apps/my_lib to your path (then the parent directory will be in the path. Therefore your custom module will be a subdirectory in a directory that is in your path)

In this particular case, then, you can probably just change your second cell to: sys.path.append("/home/inetvmart/python_apps")


I must warn you that this is a somewhat ... hacky ... approach (modifying the path inside a Python script to ensure your imports succeed).

In the medium term, you may like to do something like:

  • create a folder called e.g. /home/<your username>/my_python_modules
  • put this path into the $PYTHONPATH environment variable (which you could set in your .bashrc/.zshrc/other file)

Then you will be able to import the custom modules inside my_python_modules from anywhere on your machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文