简体   繁体   English

如何设置我的导入以运行我的 python 应用程序而不安装它,并且仍然能够使用诗歌运行 tox?

[英]How can I set up my imports in order to run my python application without installing it, and still be able to run tox using poetry?

I have a python 3.6 code-base which needs to be installed in the environment's site-packages directory in order to be called and used.我有一个 python 3.6 代码库,需要将其安装在环境的站点包目录中才能被调用和使用。 After moving it to docker, I decided that I should set up a shared volume between the docker container and the host machine in order to avoid copying and installing the code on the container and having to rebuild every time I made a change to the code and wanted to run it.将其移至 docker 后,我决定在 docker 容器和主机之间设置一个共享卷,以避免在容器上复制和安装代码,并且每次更改代码时都必须重新构建想运行它。 In order to achieve this, I had to change a lot of the import statements from relative to absolute.为了实现这一点,我不得不将很多导入语句从相对更改为绝对。 Here is the structure of the application:这是应用程序的结构:

-root
  -src
    -app
  -test

In order to run the application from the root directory without installing it, I had to change a lot of the import statements from为了从根目录运行应用程序而不安装它,我不得不从

from app import something

to:至:

import src.app.something

The problem is that I use poetry to build the app on an azure build agent, and tox to run the tests.问题是我使用诗歌在 azure 构建代理上构建应用程序,并使用 tox 来运行测试。 The relevant part of my pyproject.toml file looks like this:我的 pyproject.toml 文件的相关部分如下所示:

[tool.poetry]
name = "app"
version = "0.1.0"
packages = [{include = 'app', from='src'}]

The relevant part of my tox.ini file looks like this:我的 tox.ini 文件的相关部分如下所示:

[tox]
envlist = py36, bandit, black, flake8, safety
isolated_build = True

[testenv:py36]
deps =
    pytest
    pytest-cov
    pytest-env
    pytest-mock
    fakeredis
commands =
    pytest {posargs} -m "not external_service_required" --junitxml=junit_coverage.xml --cov report=html --cov-report=xml:coverage.xml

I'm not an expert in tox or poetry, but from what I could tell, the problem was that the src directory wasn't being included in the build artifact, only the inner app directory was, so I added a parent directory and changed the directory structure to this:我不是 tox 或诗歌方面的专家,但据我所知,问题是 src 目录没有包含在构建工件中,只有内部 app 目录包含,所以我添加了一个父目录并更改了目录结构到这个:

-root
  -app
    -src
      -app
  -test

And then changed the poetry configuration to the following in order to include the src directory然后为了包含 src 目录,将诗歌配置改为如下

[tool.poetry]
name = "app"
version = "0.1.0"
packages = [{include = 'src', from='app'}]

Now when I change the imports in the tests from this:现在,当我从这里更改测试中的导入时:

from app import something

to this:对此:

from app.src.app import something 

The import is recognized in Pycharm, but when I try to run tox -r, the I get the following error:导入在 Pycharm 中被识别,但是当我尝试运行 tox -r 时,我收到以下错误:

E   ModuleNotFoundError: No module named 'app'

I don't understand how tox installs the application, and what kind of package structure I need to specify in order to be able to call the code both from the code-base directory and from site packages.我不明白 tox 如何安装应用程序,以及我需要指定哪种 package 结构才能从代码库目录和站点包中调用代码。 I looked at some example projects, and noticed that they don't use the isolated_build flag, but rather the skip_dist flag, but somehow they also install the application in site packages before running their tests.我查看了一些示例项目,并注意到它们不使用isolated_build 标志,而是使用skip_dist 标志,但不知何故,他们还在运行测试之前将应用程序安装在站点包中。

Any help would be much appreciated.任何帮助将非常感激。

Specs:眼镜:

poetry version: 1.1.6
python version:3.6.9 
tox version:3.7
environment: azure windows build agent

You have to change the imports back to from app import something , the src part is, with respect to the code as a deliverable, completely transient.您必须将导入更改回from app import something ,就作为可交付成果的代码而言, src部分是完全瞬态的。 Same goes for adding in another app directory, your initial project structure was fine.添加另一个app目录也是如此,您的初始项目结构很好。

You were right about going from relative imports to absolute ones though, so all that is necessary thereafter is telling your python runtime within the container that root/src should be part of the PYTHONPATH :不过,您从相对导入到绝对导入是正确的,因此此后所需要做的就是告诉容器中的 python 运行时root/src应该是PYTHONPATH的一部分:

export PYTHONPATH="{PYTHONPATH}:/path/to/app/src"

Alternatively, you can also update the path within your python code right before importing your package:或者,您也可以在导入 package 之前更新 python 代码中的路径:

import sys
sys.path.append("/path/to/root/src")

import app  # can be found now

Just to state the obvious, meddling with the interpreter in this way is a bit hacky, but as far as I'm aware it should work without any issues.就 state 而言,显而易见,以这种方式干预解释器有点笨拙,但据我所知,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Azure 容器应用程序中运行我的应用程序 - Not able to Run my Application in Azure container Apps 如何连续运行我的 python 程序 - How can I run my python program continuously 我如何设置我的Azure帐户以通过REST API进行ARM部署? - How can I set up my Azure account in order to make ARM deployments via the REST API? 如何在 azure 的 windows 容器上运行我的应用程序? - how to run my application on windows container in azure? 如何使用我自己的自定义 AuthorizationLevel 在 azure 中运行 function? - How can i run a function in azure using my own custom AuthorizationLevel? 如何在本地计算机上使用azure运行terraform init - How can I run terraform init with azure on my local machine 如果我的函数未在 24 小时内被调用,我是否可以设置 Azure Application Insight 警报? - Can I set up an Azure Application Insight alert if my function wasn't called within 24 hours? 如何/无需在云中手动运行RDP即可运行Azure VM? - How do i/is it possible to run my Azure VM without running the RDP manually aka somewhere in the cloud? 如何设置我的 Linux Azure VM 以便我可以通过浏览器连接? - How do I set up my Linux Azure VM so that I can connect via a browser? 如何查看刚刚在Azure上设置的Docker化容器应用程序? - How can i view my dockerized container app that i just set up on Azure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM