简体   繁体   English

python模块的中央路径

[英]central path for python modules

I am starting to convert a lot of C stuff in python 3. 我开始在python 3中转换很多C东西。

In CI defined a directory called "Toolbox", where i put all my functions i needed in different programs, so called libraries. 在CI中定义了一个名为“工具箱”的目录,在该目录中,我需要的所有功能都放在不同的程序中,即所谓的库。

To use a specific library i had just to add the line 要使用特定的库,我只需要添加行

#include "/home/User/Toolbox/VectorFunctions.h"

into my source. 进入我的来源。 So i was able to use the same library in different sources. 因此,我能够在不同来源中使用相同的库。

In python i tried to write some Toolbox functions and implement them into the source with import VectorFunctions, which works, as long as the file VectorFunctions.py is in the same directory as the source. 在python中,我尝试编写一些Toolbox函数,并使用import VectorFunctions将其实现到源代码中,只要文件VectorFunctions.py与源文件位于同一目录中,该函数就可以工作。

I there a way (I think there must be one...) telling python that VectorFunctions.py is located in a different directory, eg /home/User/Python_Toolbox? 我有一种方法(我认为必须有一个...)告诉python VectorFunctions.py位于另一个目录中,例如/ home / User / Python_Toolbox?

Thanks for any comment! 感谢您的任何评论!

What I would do is to organize these toolbox functions into one installable Python package bruno_toolbox , with its setup.py , and then install it into development mode to system site packages, using python setup.py develop , and then use the bruno_toolbox like any other package on the system, everywhere. 我要做的是将这些工具箱功能组织到一个带有setup.py安装Python程序包bruno_toolbox ,然后使用python setup.py develop development将其安装到开发模式下的系统站点程序包中,然后像其他任何bruno_toolbox一样使用bruno_toolbox系统上的软件包,无处不在。 Then if that package feels useful, I'd publish it to PyPI for the benefit of everyone. 然后,如果觉得该软件包有用,那么我将其发布给PyPI,以使所有人受益。

You can use python path. 您可以使用python路径。 Writing this code beginning of your program : 在程序开始处编写此代码:

import sys
sys.path.append('/home/User/Python_Toolbox')

If you have VectorFunctions.py in this folder you can import it : 如果此文件夹中有VectorFunctions.py ,则可以将其导入:

import VectorFunctions

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

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