简体   繁体   English

pip install -r requirements.txt 没有扩展我的环境变量

[英]pip install -r requirements.txt doesn't expand my environment variables

I have private package in a Gitlab registry.我在 Gitlab 注册表中有私有 package。 In order to install it I had to create a token.为了安装它,我必须创建一个令牌。

When I run the following command:当我运行以下命令时:

pip install my-package-name --index-url https://${GITLAB_TOKEN_USER}:${GITLAB_TOKEN}@gitlab.com/api/v4/projects/12345678/packages/pypi/simple

The package get installed properly. package 安装正确。 With following lines (we can see that the ENV var where expanded properly)使用以下几行(我们可以看到 ENV var 已正确扩展)

Looking in indexes: https://gitlab%2Bdeploy-token-564332:****@gitlab.com/api/v4/projects/1234567/packages/pypi/simple
Collecting my-package-name.
  Downloading https://gitlab.com/api/v4/projects/12345678/packages/ ...
....

But when i try to install through pip install -r requirements.txt .但是当我尝试通过pip install -r requirements.txt时。 I get this message:我收到这条消息:

Looking in indexes: https://%24%7BGITLAB_TOKEN_USER%7D:****@gitlab.com/api/v4/projects/1234567/packages/pypi/simple
WARNING: 401 Error, Credentials not correct for https://gitlab.com/api/v4/projects/1234567/packages/pypi/simple/my-package-name/
ERROR: Could not find a version that satisfies the requirement my-package-name (from versions: none)
ERROR: No matching distribution found for my-package-name

And it seems that when I use this command the ENV var doesn't get expanded.似乎当我使用此命令时,ENV var 没有得到扩展。

My requirements.txt looks like this:我的requirements.txt看起来像这样:

--index-url https://${GITLAB_TOKEN_USER}:${GITLAB_TOKEN}@gitlab.com/api/v4/projects/1234567/packages/pypi/simple

my-package-name

I'm using pip version 22.3.1我正在使用 pip 版本 22.3.1

Any why this is happenning?为什么会这样? And if there is any other way to install multiple private packages without hardcoding my token in the requirements.如果有任何其他方法可以安装多个私有包而无需在要求中对我的令牌进行硬编码。

As mentioned by @Abdul Aziz Barkat my environment variable were not set.正如@Abdul Aziz Barkat 所提到的,我的环境变量没有设置。

I used source.env to load my variables.我使用source.env来加载我的变量。 And when I was checking with echo $MY_ENV my var were displaying.当我用echo $MY_ENV检查时,我的 var 正在显示。 So I though it was ok.所以我觉得还可以。

But when I used cat <(envsubst < requirements.txt) to check for my variables.但是当我使用cat <(envsubst < requirements.txt)检查我的变量时。 They were replaced in my requirements.txt by an empty string.它们在我的 requirements.txt 中被替换为空字符串。

As pointed by @Charles Duffy, in order to export my var from my .env file I should use:正如@Charles Duffy 所指出的,为了从我的.env文件中导出我的 var,我应该使用:

set -a
source .env
set +a

this source helped me: https://ostechnix.com/difference-between-defining-bash-variables-with-and-without-export/这个来源帮助了我: https://ostechnix.com/difference-between-defining-bash-variables-with-and-without-export/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM