在哪里保存我的自定义脚本,以便我的 python 脚本可以访问默认目录中的模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20843088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:21:57  来源:igfitidea点击:

Where to save my custom scripts so that my python scripts can access the module in the default directory?

pythonimportdirectorynative

提问by alvas

This is going to be a multi-part question but the ultimate aim is such that I can access custom-made modules/libraries/functions like how I do in native python.

这将是一个多部分问题,但最终目标是让我可以访问定制的模块/库/函数,就像我在本机 python 中所做的那样。

Where are the non-native but pipinstalled python libraries stored and how to configure my interpreter/IDE to access them?

非本地但pip已安装的 Python 库存储在哪里以及如何配置我的解释器/IDE 以访问它们?

My users' script all starts with:

我的用户脚本都以:

#!/usr/bin/env python -*- coding: utf-8 -*- 

What's the difference between accessing from /usr/binand /usr/bin/env, will the custom-made modules that should import like native python modules/packages work?

访问 from/usr/bin和之间有什么区别/usr/bin/env,应该像本机 python 模块/包一样导入的定制模块是否工作?

Should my custom scripts become packages? if so how do I make the user-side code, checks for ImportErrorand install/setup these packages in the try-except?e.g.

我的自定义脚本应该成为包吗?如果是这样,我如何制作用户端代码、检查ImportError和安装/设置这些软件包try-except例如

try:
  import module_x
except ImportError:
  # Install package, but how to do it within the script?
  pass

Is there a place to store my custom scripts such that it imports like a native library?If so, where? What are the consequences?

有没有地方可以存储我的自定义脚本,以便像本机库一样导入?如果有,在哪里?后果是什么?

采纳答案by dg99

Well, you've asked a lot of questions; I will address the one in the subject line.

嗯,你问了很多问题;我将解决主题行中的问题。

You can put Python module files anywhere you want and still importthem without any problems as long as they are in your module search path. You can influence your module search path by altering the environment variable PYTHONPATHin your shell before invoking Python, or by altering the sys.pathvariable inside your code.

你可以把 Python 模块文件放在任何你想要的地方import,只要它们在你的模块搜索路径中,它们仍然没有任何问题。您可以通过PYTHONPATH在调用 Python 之前更改 shell 中的环境变量或通过更改代码中的sys.path变量来影响模块搜索路径。

So if you've installed /home/alvas/python/lib/module_x.pyand /usr/local/python/lib/foo.pyyou could run:

因此,如果您已经安装/home/alvas/python/lib/module_x.py并且/usr/local/python/lib/foo.py可以运行:

PYTHONPATH=/home/alvas/python/lib:/usr/local/python/lib /home/alvas/scripts/bar.py

and then the statements

然后是陈述

import module_x
import foo

should simply work.

应该简单地工作。

Alternatively you could do something like this in your code:

或者,您可以在代码中执行以下操作:

#!/usr/bin/env python
import sys
sys.path.append('/home/alvas/python/lib')
import module_x
sys.path.append('/usr/local/python/lib')
import foo

Either approach will work.

任何一种方法都会奏效。