简体   繁体   English

将 Poetry 与 PyEnv 结合使用并遇到 Python 版本问题

[英]Using Poetry with PyEnv and having Python version issues

I'm working on WSL2 Ubuntu.我正在开发 WSL2 Ubuntu。 I've been taking a course in backend API development using Fastapi.我一直在学习使用 Fastapi 进行后端 API 开发的课程。 My Ubuntu default python is 3.8 I believe.我相信我的 Ubuntu 默认 python 是 3.8。 I'm trying to use python 3.10.0 for my development.我正在尝试使用 python 3.10.0 进行开发。 I've done the following:我做了以下事情:

  1. pyenv install 3.10.0 pyenv 安装 3.10.0
  2. cd to my project folder and pyenv local 3.10.0 (creates.python-version file containing 3.10.0) cd 到我的项目文件夹和 pyenv local 3.10.0 (creates.python-version file containing 3.10.0)
  3. poetry shell (this creates a.venv folder and all dependencies are added there during development). poetry shell(这会创建一个 .venv 文件夹,并且在开发过程中会在其中添加所有依赖项)。 Bash Prompt has (.venv) in it. Bash Prompt 中有 (.venv)。
  4. python --version shows 3.10.0 as current version python --version 显示 3.10.0 作为当前版本

My problem is that when I look into the.venv/bin file, all of the symbolic links show python 3.8.我的问题是,当我查看 .venv/bin 文件时,所有符号链接都显示 python 3.8。

drwxr-xr-x 2 mryan mryan 4096 Feb  1 23:12 .
drwxr-xr-x 4 mryan mryan 4096 Feb  1 23:12 ..
-rw-r--r-- 1 mryan mryan 2152 Feb  1 23:12 activate
-rw-r--r-- 1 mryan mryan 1444 Feb  1 23:12 activate.csh
-rw-r--r-- 1 mryan mryan 3033 Feb  1 23:12 activate.fish
-rw-r--r-- 1 mryan mryan 1303 Feb  1 23:12 activate.nu
-rw-r--r-- 1 mryan mryan 1754 Feb  1 23:12 activate.ps1
-rw-r--r-- 1 mryan mryan 1199 Feb  1 23:12 activate_this.py
-rw-r--r-- 1 mryan mryan  333 Feb  1 23:12 deactivate.nu
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip-3.8
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip3
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip3.8
lrwxrwxrwx 1 mryan mryan   18 Feb  1 23:12 python -> /usr/bin/python3.8
lrwxrwxrwx 1 mryan mryan    6 Feb  1 23:12 python3 -> python
lrwxrwxrwx 1 mryan mryan    6 Feb  1 23:12 python3.8 -> python
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel-3.8
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel3
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel3.8

When I try using the command poetry env use 3.10.0 I get the following:当我尝试使用命令 poetry env use 3.10.0 时,我得到以下信息:

(.venv) mryan ~/python/fastapi1/.venv/bin $poetry env use 3.10.0
/bin/sh: 1: python3.10: not found

  EnvCommandError

  Command python3.10 -c "import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))" errored with the following return code 127, and output:


  at ~/.local/share/pypoetry/venv/lib/python3.8/site-packages/poetry/utils/env.py:378 in activate
       374│                     shell=True,
       375│                 )
       376│             )
       377│         except CalledProcessError as e:
    →  378│             raise EnvCommandError(e)
       379│
       380│         python_version = Version.parse(python_version.strip())
       381│         minor = "{}.{}".format(python_version.major, python_version.minor)
       382│         patch = python_version.text
(.venv) mryan ~/python/fastapi1/.venv/bin $python3.10
python3.10: command not found

What am I doing wrong here?我在这里做错了什么? I feel I'm missing something significant here.我觉得我在这里遗漏了一些重要的东西。 I've been through a zillion web pages on poetry and virtual environments.我浏览过无数关于诗歌和虚拟环境的网页。

I had the same problem you are having, I used to be able to use:我遇到了同样的问题,我曾经能够使用:

pyenv local 3.10.0

poetry new example-project

And poetry would create example-project with a venv in it based on python 3.10.0. poetry将基于 python 3.10.0 创建带有 venv 的example-project This no longer worked for me so I found that these commands did work for me:这不再对我有用,所以我发现这些命令我有用:

poetry new example-project
cd example-project/
poetry env use 3.10.0
poetry update

In case your project already had a venv installed it might be a good idea to remove that first before using the poetry env command.如果您的项目已经安装了 venv,那么在使用poetry env命令之前先删除它可能是个好主意。 I'm pretty sure you haven't added pyenv to your PATH properly.我很确定您没有正确地将pyenv添加到您的PATH中。 Check out the installation instructions for pyenv , specifically the ' Configure your shell's environment for Pyenv' section.查看pyenv安装说明,特别是“为 Pyenv配置 shell 环境”部分。

I am surprised that poetry shell creates .venv .我很惊讶poetry shell创建.venv Are you certain about that?你确定吗? Or did you maybe create it with virtualenv ?或者您是否使用virtualenv创建了它?

I just kind of replicated what you probably did and did not get the error.我只是复制了你可能做过的事情,但没有得到错误。 I would recommend you to create a minimal, reproducible example .我建议您创建一个最小的、可重现的示例 Then you will probably catch what is wrong.然后你可能会发现什么是错误的。 For this I case I recommend creating a bash script.对于这种情况,我建议创建一个 bash 脚本。 A start could be:开始可能是:

mkdir ~/python/test_error
cd ~/python/test_error
cp ~/python/fastapi1/pyproject.toml .
pyenv local 3.10.0
poetry env use 3.10.0

This script works for me (when I change the folders).该脚本适用于我(当我更改文件夹时)。

I'm able to replicate your exact error using these commands:我可以使用以下命令复制您的确切错误:

poetry new test-project

cd test-project/

poetry env use 3.10

Result in the exact same error you are getting.导致您得到完全相同的错误。 However, this works for me:但是,这对我有用:

poetry new test-project

cd test-project/

pyenv local 3.10

poetry env use 3.10

Following these commands my poetry will create a .venv inside the project's folder successfully.按照这些命令,我的poetry将在项目文件夹中成功创建一个.venv By the way, you'll have to edit the pyproject.toml file manually, because that doesn't change using this method.顺便说一句,您必须手动编辑pyproject.toml文件,因为使用此方法不会改变。

If this doesn't work I suspect something went wrong while adding pyenv to your PATH .如果这不起作用,我怀疑在将pyenv添加到PATH时出现问题。 In that case you could try resetting your 'dot-files' ( .bashrc , .bash_profile and/or .profile ) and add pyenv again using these commands from the pyenv github :在这种情况下,您可以尝试重置您的“点文件”( .bashrc.bash_profile和/或.profile )并使用 pyenv github 中的这些命令再次添加pyenv

# the sed invocation inserts the lines at the start of the file
# after any initial comment lines
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Thanks SWHJ, I also come up with that solution.感谢 SWHJ,我也想出了这个解决方案。 Just a small point here is we need need to set this config这里有一点是我们需要设置这个配置

poetry config settings.virtualenvs.in-project true诗歌配置 settings.virtualenvs.in-project true

so poetry will create a.venv inside the project's folder所以 poetry 将在项目文件夹中创建 a.venv

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

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