English 中文(简体)
雷达——在指挥系统运行期间增加PYTHONPATH
原标题:Python - add PYTHONPATH during command line module run

我想说的是:

python somescript.py somecommand

但是,当我执政时,需要<代码>PYTHONPATH,以列入某一名录。 我只能把它添加到我的环境变量上,因为我的名录希望根据项目I m的运行情况增加变化。 是否有办法更改<代码> PYTHONPATH, 同时又使用文字? 注:我甚至没有<代码>。 缩略语

最佳回答

关于Mac/HCH;

PYTHONPATH=/foo/bar/baz python somescript.py somecommand

视窗设计一个包裹<代码>pythonpath.bat;

@ECHO OFF
setlocal
set PYTHONPATH=%1
python %2 %3
endlocal

电话:pythonpath.bat 文字档案类似;

pythonpath.bat /foo/bar/baz somescript.py somecommand
问题回答
import sys
sys.path.append( your certain directory )

基本玩具.path是一份清单,有所有探照灯模块。 最初由口译人员进行。 PYTHONPATH的内容在该清单的末尾自动添加。

如果你从一个符合规则的飞机壳上指挥,如<条码>bash,你可以将环境变数确定如下:

PYTHONPATH="/path/to" python somescript.py somecommand

如果它一行不变,PYTHONPATH环境值只适用于该指挥部。

$ echo $PYTHONPATH

$ python -c  import sys;print("/tmp/pydir" in sys.path) 
False
$ PYTHONPATH=/tmp/pydir python -c  import sys;print("/tmp/pydir" in sys.path) 
True
$ echo $PYTHONPATH

您可在您希望进口的一揽子计划_init_py上贴出以下代码:

import sys
from pathlib import Path

sys.path.insert(0, Path(__file__).parent)

然后,你可以按照你通常的做法从一揽子计划中进口单元。

import <module_name>

<module_name>.<method_on_module>()
Example:
import algorithms

algorithms.run_algo1()

你的民主选举学会可能强调单元名称,但当你解释“灰色”时,这就是一个问题。

此外,如果你需要读到这条道路,你可以做:

DIR_PATH = Path(sys.path[0])

您可尝试在文稿中履行职务。

python -c "import sys; sys.path.append( /your/script/path ); import yourscript; yourscript.yourfunction()"

窗口:

例如,在我的台上,我有一个名为“制图员”的夹。 另外,有几卷“缩略语”和“图表”,其中载有我的主要档案“图表”。 此外,“图表”储存在“图表”。 如果没有转移档案,我就能够打字。 请打电话给这一批号“图表”。

@ECHO OFF
setlocal
set PYTHONPATH=%cd%grapher;%cd%calculation
python %cd%graphergrapherMain.py
endlocal

该书位于“图表”。 为了管理事情,我会立即接受我的指挥,然后:

>cd Desktopmygrapher (this navigates into the "mygrapher" folder)
>rungraph.bat (this executes the batch file)




相关问题
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 ]="...

热门标签