用于项目目录和易于导入的Python文件夹结构 [英] Python folder structure for project directory and easy import
问题描述
我的团队在python3中有一个包含几个小项目的文件夹.其中,我们有一个带有多个实用程序功能的实用程序文件夹,这些文件夹在整个项目中都使用.但是导入它的方式非常不舒服.这是我们使用的结构:
My team has a folder of several small projects in python3. Amongst them, we have a utility folder with several utility functions, that are used throughout the projects. But the way to import it is very uncomfortable. This is the structure we use:
temp_projects
util
storage.py
geometry.py
project1
project1.py
project2
project2.py
问题在于项目中的导入看起来很糟糕:
The problem is that the import in the projects looks terrible:
sys.path.insert(1, os.path.join(sys.path[0], '..'))
import util.geometry
util.geometry.rotate_coordinates(....)
此外,pycharm和其他工具在理解它和提供补全方面也遇到了麻烦.
Also, pycharm and other tools are having trouble understanding it and to supply completion.
有一些更整洁的方法吗?
Is there some neater way to do that?
所有的项目和utils都在开发中,并且经常进行修改,因此我正在寻找尽可能灵活和舒适的东西
All of the projects and utils are very much work-in-progress and are modified often, so I'm looking for something as flexible and comfortable as possible
推荐答案
PYTHONPATH
环境变量可能是一种解决方法.只需将其设置为项目文件夹位置:
PYTHONPATH
environment variable might be a way to go. Just set it to projects folder location:
PYTHONPATH=/somepath/temp_projects
,您将可以按以下方式使用 util
:
and you'll be able to use util
as follows:
import util.geometry
util.geometry.rotate_coordinates(....)
PyCharm也会自动识别.
Also this will be recognized by PyCharm automatically.
这篇关于用于项目目录和易于导入的Python文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!