将代码从开发计算机移动到目标时管理 Python 路径

发布于 2024-08-03 14:26:36 字数 1050 浏览 9 评论 0原文

我有一个具有以下目录结构和这些文件的 python 项目:

/home/project_root
|---__init__.py
|---setup
       |---__init__.py
       |---configs.py
|---test_code
       |---__init__.py
       |---tester.py

测试器脚本从 setup/configs.py 导入,并引用“setup.configs”。它在我的开发机器上运行良好。

这适用于开发 (Linux) 计算机。当我将其移动到另一台(Linux)计算机时,我设置了 PYTHONPATH

PYTHONPATH = "/home/project_root"

但当我运行 tester.py 时,它找不到配置模块。当我运行交互式 Python 解释器时,sys.path 不包含 /home/project_root 目录。但是当我回显 $PYTHPATH 时, /home/project_root 确实出现了。

我在这里做错了什么?

(我不想依赖 .bashrc 文件来设置目标机器的 PYTHONPATH ——代码是针对 Django 应用程序的,最终将由 www-data 运行。而且,我知道 apache 配置Django 包含 PYTHONPATH 规范,但我不想在这里使用它,因为我首先尝试确保代码在目标计算机环境中通过单元测试。)

CURIOUSER 和 CURIOUSER 这似乎是用户 ID 和权限问题。 - 当通过普通用户的命令启动时,解释器可以按预期导入模块。 - 当通过 sudo 启动时(我在这里运行 Ubuntu),解释器无法按预期导入模块。 - 我一直在使用 sudo 调用测试脚本,因为这些文件归 www-data 所有(b/c 它们将由运行 apache 作为 Django 应用程序一部分的用户调用)。 - 将文件的所有权更改为普通用户的所有权后,测试脚本运行时不会出现导入错误(尽管会出现各种与用户 ID 相关的墙)。

抱歉浪费您的时间。 这个问题应该结束。

I have a python project with this directory structure and these files:

/home/project_root
|---__init__.py
|---setup
       |---__init__.py
       |---configs.py
|---test_code
       |---__init__.py
       |---tester.py

The tester script imports from setup/configs.py with the reference "setup.configs". It runs fine on my development machine.

This works on the development (Linux) computer. When I move this to another (Linux) computer, I set the PYTHONPATH with

PYTHONPATH = "/home/project_root"

But when I run tester.py, it can't find the configs module. And when I run the interactive Python interpreter, sys.path doesn't include the /home/project_root directory. But /home/project_root does appear when I echo $PYTHPATH.

What am I doing wrong here?

(I don't want to rely on the .bashrc file to set the PYTHONPATH for the target machine -- the code is for a Django application, and will eventually be run by www-data. And, I know that the apache configuration for Django includes a specification of the PYTHONPATH, but I don't want to use that here as I'm first trying to make sure the code passes its unit tests in the target machine environment.)

CURIOUSER AND CURIOUSER
This seems to be a userid and permissions problem.
- When launched by a command from an ordinary user, the interpreter can import modules as expected.
- When launched by sudo (I'm running Ubuntu here), the interpreter cannot import modules as expected.
- I've been calling the test script with sudo, as the files are owned by www-data (b/c they'll be called by the user running apache as part of the Django application).
- After changing the files' ownership to that of an ordinary user, the test script does run without import errors (albeit, into all sorts of userid related walls).

Sorry to waste your time. This question should be closed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-08-10 14:26:36

将其粘贴在测试脚本中的 import setup.configs 之前

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir))

sys.path 是 python 解释器在导入 python 模块时查找的所有目录的列表。
这会将包含安装模块的父目录添加到该列表的开头,这意味着将首先检查本地目录。如果您在系统范围内安装了模块,这一点很重要。有关详细信息,请访问:系统文档

编辑:您还可以将 .pth 文件放入 /usr/local/lib/python2.X/site-packages/ .pth 文件只是一个文本文件,其中包含python 解释器将在其中搜索的每一行的目录路径。因此只需添加一个包含此行的文件:

/home/project_root

Stick this in the tester script right before the import setup.configs

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir))

sys.path is a list of all the directories the python interpreter looks for when importing a python module.
This will add the parent directory which contains setup module to the beginning of that list which means that the local directory will be checked first. That is important if you have your module installed system wide. More info on that here: sys doc.

EDIT: You could also put a .pth file in /usr/local/lib/python2.X/site-packages/ A .pth file is simply a text file with a directory path on each line that the python interpreter will search in. So just add a file with this line in it:

/home/project_root
唔猫 2024-08-10 14:26:36

尝试在脚本中显式设置 Python 路径。如果您不想更改它,您可以随时将“../”之类的内容添加到测试器中的路径中。也就是说:

sys.path.append("../")

Try explicitly setting your python path in your scripts. If you don't want to have to change it, you could always add something like "../" to the path in tester. That is to say:

sys.path.append("../")
ゞ花落谁相伴 2024-08-10 14:26:36

(我不想依赖 .bashrc 文件来设置目标机器的 PYTHONPATH ——该代码是针对 Django 应用程序的,最终将由 www-data 运行。而且,我知道Django 的 apache 配置包含 PYTHONPATH 规范,但我不想在这里使用它,因为我首先尝试确保代码在目标计算机环境中通过单元测试。)

如果代码用于 Django 应用程序,则为您没有在 Django 项目上下文中测试它有什么原因吗?在 Django 项目上下文中测试它有几个好处:

  1. Django 的 manage.py 将为您设置 Python 环境。它将向 sys.path 添加适当的项目路径,并正确设置环境变量 DJANGO_SETTINGS_MODULE
  2. Django 的库包含充足的单元测试工具,您可以轻松扩展该功能以包含您自己的测试工具。在 Django 项目中执行测试就像通过 manage.py 执行单个命令一样简单。

(I don't want to rely on the .bashrc file to set the PYTHONPATH for the target machine -- the code is for a Django application, and will eventually be run by www-data. And, I know that the apache configuration for Django includes a specification of the PYTHONPATH, but I don't want to use that here as I'm first trying to make sure the code passes its unit tests in the target machine environment.)

If the code is for a Django application, is there a reason you're not testing it in the context of a Django project? Testing it in the context of a Django project gives a couple benefits:

  1. Django's manage.py will set up your Python environment for you. It'll add the appropriate project paths to sys.path, and it'll set the environment variable DJANGO_SETTINGS_MODULE correctly.
  2. Django's libraries include ample unit testing facilities, and you can easily extend that functionality to include your own testing facilities. Executing tests in a Django project is as easy as executing a single command via manage.py.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文