English 中文(简体)
Best Way to Other Importlement in ecosoc PYTHONPATH 难道不能修改吗?
原标题:Best Way to Import Other Modules in Python when PYTHONPATH Cannot Be Modified?

我把我的第一个项目 in在平地,我不敢处理进口问题。 我在大学从事电脑工作,不能修改PYTHONPATH变量。 我也在从各种计算机/系统着手开展这一项目(因此,项目的道路并不总是一样)。

我在不同的夹中有许多不同的模块,每个单元都进口。 目前,我通过使用<编码>file_path = os.path.abspath(__file__)sys.path.append(symantic_root)。

该系统运行良好,但最终发现非常可观,每个单元一开始就有许多重复编码,例如:

import os
import sys
# Get the path to the directory above the directory this file is in, for any system
file_path = os.path.abspath(__file__)
root_path = os.path.dirname(os.path.dirname(file_path))
# Get the paths to the directories the required modules are in
symantic_root = os.path.join(root_path, "semantic_analysis")
parser_root = os.path.join(root_path, "parser")
# Add the directories to the path variable
sys.path.append(symantic_root)
sys.path.append(parser_root)
import semantic_analyser
import jaml

如能就如何以更好的方式安排这样的项目提出建议,将受到高度赞赏。

最佳回答

首先,创设一个简单的<代码>main.py,为您的申请提供一个单一的切入点。 例如:

if __name__ ==  __main__ :

    import sys   
    from package import app

    sys.exit(app.run())

其次,建立一个把所有单元组合在一起的高层一揽子方案,并为其提供一个接入点:,作为main.py。 然后,你应当能够消除所有道路操纵代码,并且简单地使用全面具体的进口说明,如从一揽子计划中的<编码>。 模块进口功能,从您申请的任何模块中获取。

问题回答

第一,在以下文件中,你读到一些关于沙捞越道路的 st子:

为了解决你的问题,你可能使用第版(python-search-path) 载有您所需模块的档案已经存在。

然后,从你的主要文字来看,你可以做些什么。

import site
site.addsitedir(".")

将自动将这些名录贴上<代码>。 PYTHONPATH。

你没有提及你重新开展工作的特别协调员。 大部分答案都预先假定了一种不光彩的环境。 如果你重在视窗上,其他人就不得不chim。

这从你的例子中看不出你为什么要重新走到所有这些阴谋中。 首先,你无法修改<代码> PYTHONPATH? 这只是一个巨大的环境变量。 如果你能够投身于沙尔,你显然能够拥有确定自己环境变量的必要机会。

But I m not even sure that s necessary. If you were to simply install all of your custom modules into your own library directory, and install .pth files as suggested by vinilios, you could just do something like this:

import site
import os

site.addsitedir(os.path.expanduser( ~/lib/python ))

您也不妨研究virtualenv的一揽子计划,其中允许你创造自己的灰色环境,供你自行安装。 当你需要安装系统灰色的模块时,这非常令人费解。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...