Poetry NoCompatiblePythonVersionFound - python

I understand that the Poetry project is working with a different version of Python than I have installed.
How might I rectify this issue though? Preferably, Poetry and the project can use the latest Python version.
Terminal
cd /mnt/c/Users/me/Documents/GitHub/project
poetry run python -m project
python3 --version
Output
The currently activated Python version 3.8.10 is not supported by the project (3.8.8).
Trying to find and use a compatible version.
NoCompatiblePythonVersionFound
Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command.
at ~/.poetry/lib/poetry/utils/env.py:768 in create_venv
764│ python_minor = ".".join(python_patch.split(".")[:2])
765│ break
766│
767│ if not executable:
→ 768│ raise NoCompatiblePythonVersionFound(
769│ self._poetry.package.python_versions
770│ )
771│
772│ if root_venv:
Python 3.8.10

I need to set up a virtual environment.
To do this in Conda:
conda create -n env_name python=3.8.8

Related

Poetry doesn't use the correct version of Python

I've recently installed both Pyenv and Poetry and want to create a new Python 3.8 project. I've set both the global and local versions of python to 3.8.1 using the appropriate Pyenv commands (pyenv global 3.8.1 for example). When I run pyenv version in my terminal the output is 3.8.1. as expected.
Now, the problem is that when I create a new python project with Poetry (poetry new my-project), the generated pyproject.toml file creates a project with python 2.7:
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["user <user#email.com>"]
[tool.poetry.dependencies]
python = "^2.7"
[tool.poetry.dev-dependencies]
pytest = "^4.6"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
It seems that Poetry defaults back to the system version of Python. How do I change this so that it uses the version installed with Pyenv?
Edit
I'm using MacOS, which comes bundled with Python 2.7. I think that might be causing some of the issues here. I've reinstalled Python 3.8 again with Pyenv, but when I hit Poetry install I get the following error:
The currently activated Python version 2.7.16 is not supported by the project (^3.8).
Trying to find and use a compatible version.
[NoCompatiblePythonVersionFound]
Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command.
Should I create an environment explicitly for the project using Pyenv or should the project be able to access the correct Python version after running pyenv local 3.8.1.? When I do the latter, nothing changes and I still get the same errors.
pyproject.toml is used to define all the dependencies for your project, including the supported python version.
The line your complaining about is just saying that the versions of python supported by the project is python2.7 or greater, this is independent of what versions of python you've installed with pyenv.
python = "^2.7"
If you want to update the versions of python supported by the project you can edit the file directly and run poetry update.
If you want to use multiple versions of python you need to make sure poetry is using the correct dependencies for the version of python you are using. To change the specific version poetry is using you should use poetry env,
poetry env list show the versions of python poetry can use
poetry env use <python> switches poetry to use that version.
For instance on my machine poetry has 3 virtual environments installed and is using the one associated with python3.6:
↪ poetry env list
sipy-a9sqc5pb-py3.6 (Activated)
sipy-a9sqc5pb-py3.7
sipy-a9sqc5pb-py3.8
I'm not sure how these virtual environments with interact with the shivs used by pyenv but their docs have a section relating to it
Managing Virtual Environments
There is a pyenv plugin named pyenv-virtualenv which comes with various features to help pyenv users to manage virtual environments created by virtualenv or Anaconda. Because the activate script of those virtual environments are relying on mutating $PATH variable of user's interactive shell, it will intercept pyenv's shim style command execution hooks. We'd recommend to install pyenv-virtualenv as well if you have some plan to play with those virtual environments.
Alright, I figured the problem. A little embarrassingly, I had not run pyenv shell 3.8.1 before running any of the other commands. Everything works now. Thank you all for your efforts.
you can specify an explicit python executable for poetry using
poetry env use <path to python executable>
This worked for me.
On my machine I was able to fix the "currently activated Python version is not supported by the project" error by reinstalling Poetry:
curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | python3 -
After that,poetry was able to find the correct version installed by pyenv.
In my case, I had to delete and recreate the virtualenv used by poetry. This is because I added the python version restrictions (e.g. python = ">=3.6.2 <3.7") after creating the virtualenv.
Steps
Delete the original one: run poetry env remove myApp-XkghI9p6-py3.6
Run any poetry step, to create it, or run poetry shell, and confirm poetry run python --version is the correct version.
Even though this issue has been resolved, I am writing this for somebody who comes across this problem again.
After all attempts my python -V always resulted in 2.7 and no discussions mentioned running pyenv shell (surprising to me!)
Adding pyenv to path
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
In my case I had to add it to .bashrc and not bash_profile.
https://ggkbase-help.berkeley.edu/how-to/install-pyenv/
Worked!
In my case, the environment was messed up in some way that poetry failed to activate the virtualenv properly.
Try using a different shell: perhaps, sh, or zsh. If everything works in that shell, this proves that your environment is as messed up as mine was :)
Use this command in both shells:
$ env
and try to spot the difference
You can remove the python version from pyproject.toml file and then run Poetry install
What worked for me was to run python3.8 -m poetry install.

How do I make my command line use a specific version of python?

I am getting started on using Zappa. However, I already had installed python 3.7 on my computer while Zappa uses 3.6. I installed python 3.6.8, but when I try to use zappa in the cmd (zappa init) it uses python 3.7 by default. How can I direct zappa to use 3.6 instead?
As mentioned in Zappa README:
Please note that Zappa must be installed into your project's virtual environment.
You should use something like virtualenv to create a virtual environment, which makes it easy to switch Python version.
If you use virtualenv, you can try create an environment by:
$ virtualenv -p /usr/bin/python3.6 venv
$ source activate venv
Then pip install zappa in this virtual environment.
I don't know about Zappa, but if you want use a specific version of python can do:
python3.6 my_program.py
and if whant use the command python with a specific version permanently, in linux modify the file /home/[user_name]/.bashrc and add the next line:
alias python=python3.6
You can use virtualenv to setup an environment using a specific Python version by:
% pip install virtualenv
% virtualenv -p python3.6 .venv
You can also use an absolute path to the Python executable if it's named the same but located in different folders.
Then switch to use the environment:
% source .venv/bin/activate
This environment uses Python 3.6, so install Zappa with pip like normal and you're good to go.
You can read more about usage of virtualenv here.

Specifying Python Version in Virtual Environment

I have installed python 3.6 at /usr/bin/python3.6 and I am trying to create a virtualenv using this version of python.
I tried virtualenv --python=/usr/bin/python3.6 testenv
and then I activated the virtualenv with source testenv/bin/activate
however when I open the python shell it says the version is 2.7.12
Below is a screenshot
If the virtualenv is activated then shouldn't it be using Python 3.6? What am I missing here?

How to specify python version used to create Virtual Environment?

My Python virtual environments use python3.6 when I create them using virtualenv
~ $ virtualenv my_env
but I need to use python3.5 as 3.6 is not currently supported by Opencv3.
I've tried using the --python=<py_version> flag when creating a virtual environment but this doesn't work.
How do I specify the python (3.x) version to install using virtualenv for Mac and/or Linux?
Assuming that you have installed python3 or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example
$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)
$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)
And last
# Directly point to any version of python binary, this can be even another virtualenv's bin/python.
$ virtualenv -p /path/to/any/bin/python new_env
Alternatively, I think you could use the specific version of Python itself to create the virtual environment. That way, you'll know for sure it's the correct version:
$ python3.5 -m venv test35
$ ./test35/bin/python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build ) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Reference at https://docs.python.org/3.5/library/venv.html
As of version 3.3, python includes a package named venv. However that package doesn't provide the same functionalities as the traditional virtualenv package.
venv allows creating virtual environments only for the version of python it's installed for.
virtualenv allows creating virtual environments for different versions of python by providing the path to the binary.
Creating virtual envs for different versions of python:
So assuming one has python 2.7 and python 3.6 installed in /path/to/ and wants to create the virtual env named respectively env-py36 with python 3.6 and env-py27 with python 2.7
# create a virtual env with python3's venv :
/path/to/python36/bin/python3 -m venv /my/python-venvs/env-py36
. /my/python-venvs/env-py36/bin/activate
# we're now running python 3's "env-py36" virtual env, we want to install the "virtualenv" package
pip install virtualenv
deactivate
# now use virtualenv to create a virtual environment for python 2.7
/my/python-venvs/env-py36/bin/virtualenv --python=/path/to/python27/bin/python /my/python-venvs/env-py27
Using python 3.3+ venv
Python 3.3+ :
/path/to/python3/bin/python3 -m venv ENV_DIR
Python 3.3 to 3.5 (deprecated in 3.6+) :
/path/to/python3/bin/pyvenv ENV_DIR
Sources:
Creating Virtual Environments
Python 3.3 venv
Python virtualenv package
I working on all ubuntu and MacOS
Ubuntu : virtualenv -p python3.6 environment_file
Mac OS : virtualenv -p python3.6 environment_file
I think it be same
I had this issue (and came here) but under Windows. Python 3.9 was installed on one system but it had issues with code developed under 3.7. I wanted to use a virtual environment to downgrade to 3.7 to help debug the issue. Using Python Launcher for Windows:
py -3.7 -m venv my_env
in the python project folder did the trick for me.
Simple and direct solution:
Just see this video (https://www.youtube.com/watch?v=hC9FBQnOv6o) and follow the python setup download instructions of a particular python version and then use virtualenv <folder_name> -p /python.exe
This command is also shown in the video too.
In Linux:
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps:
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
Using anaconda we can create a virtual environment called "py35_env" with Python 3.5 version by running:
conda create --name py35_env python=3.5

Setting up Django environments for Python2.X and 3.X?

I'm trying to set up a Django environment to work on a website project for my Python group. We're using 2.7 for the project, but when I followed this guide https://www.howtoforge.com/tutorial/django-install-ubuntu-14.04/ it only set it up for 3.4. How can I differentiate which set of setup tools it installs?
Here is my pip version when I type --version
~ $ pip --version
pip 1.5.6 from /usr/local/lib/python3.4/dist-packages/pip-1.5.6-py3.4.egg (python 3.4)
you can point to python executable via -p:
virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>
while creating your new virtualenv.

Categories