I am new to Python and couldn't find this answer online.
When creating a Python Flask application, what exactly determines the Python version used? I inherited an existing Python Flask application that is currently version 3.6 and wanted to upgrade it to a newer version.
Is this as simple as changing the Python version that is installed on the environment that runs the Python application or is the specific version defined in a project file?
I have tried updating the version of Python in my local environment but the project version did not change.
What you might have right now is python3 corresponding to 3.6 and python corresponding to 2.x.
You could install a new version of python; in this case 3.8 by downloading it from python website here and install it as python3.8 instead of replacing python3 or python.
Once installed, you could run virtual env with the -p or --python and passing the path to the executable as the argument as follows
python3.8 -m venv path/for/venv/py3.8
Once it's set up, source it and check out the python version.
what exactly determines the Python version used?
You can write whichever Python style you want in the project (2.x or 3.x) as long as it is supported for the version of Flask you are using.
What "determines" the version it executes with is which version of Python you specify when you start Flask. As in "python app.py" or "python3 app.py" will start with either version etc.
Related
I'm trying to run a program with poetry but it keeps using the wrong version of python.
when I use
poetry shell
It outputs
The currently activated Python version 3.9.5 is not supported by the project (>=3.10,<3.11).
Trying to find and use a compatible version.
Using python3 (3.10.4)
Which is what I want. It SHOULD use python 3.10. But I don't know why it thinks python 3.9.5 is "activated." But when I use
poetry env info
It gives me
Virtualenv
Python: 3.9.5
Implementation: CPython
Path: /Users/myname/Library/Caches/pypoetry/virtualenvs/app_name-dNeoDE2I-py3.10
Valid: True
Which is strange, it has a file called py3.10 but it sees as 3.9 and when I use it it runs as 3.9
--------------------------------------------------------------------------------------------------------
I've tried a couple things to remedy this.
poetry env use /path/to/my/python/3.10/verion/that/works/outside/poetry
Yet, this changes nothing
You need to run this:
poetry config virtualenvs.prefer-active-python true
Next, remove and create the python virtual environment again. This is what worked for me.
See the documentation please: https://python-poetry.org/docs/configuration/#virtualenvsprefer-active-python-experimental
So I just installed the latest version of python, and I also changed the old 3.5 python version to 3.10 in my environment variables. There is no python 3.5 in the PATH inside environment variables, but when I run py --version in my CMD then it says version 3.5, but when I type python --version then it says version 3.10.2, is there a way to manually choose which version should be called on py or python?
You would need a python virtual environment to control it. (venv)
Just one thing, you can create a venv only with the version or python which is installed.
Also you can use conda to manage different version of python.
Here is a tutorial to create a venv or use conda on windows :
https://medium.com/co-learning-lounge/create-virtual-environment-python-windows-2021-d947c3a3ca78#a904
#The Mungax I am not sure but I can show you something that might help you
what is the Difference between “python” vs “py”
PYTHON
The command python refers to the Python executable of the default
Python installation. Technically, the path of this version is stored
inside the PATH environment variable where your OS will search for the
executable when processing any command.
PY
The command py refers to the Python launcher, a utility that’s
automatically installed into C:\Windows\ for any Python installation
on Windows. All files in the Windows folder are accessible without
needing to modify the PATH environment variable. Thus, the Python
launcher automatically delegates the work to the latest Python version
installed in your environment. However, you can also specify the used
installation by means of a flag argument such as in py -3.6 to specify
Python version 3.6.
How to use Python 3.8 and 3.7 on Windows 10. I want to make two applications, but one requires 3.8 and another one 3.7.
So how to manage both versions in one Windows 10.
You should just install Python 3.7 and Python 3.8 and make sure that the Python Launcher for Windows is also installed (this is the default).
Then you could run your scripts using py -3.7 main.py or py -3.8 main.py to run main.py using Python versions 3.7 or 3.8, respectively.
Alternatively (even better actually), you could write the required Python version in the first line of your Python script:
#!/usr/bin/env python3.7
Note that this must be the first line of the script, otherwise it doesn't work. Now just running py main.py would automatically select the correct Python version to execute the script.
NB: If the script is executed in Linux it would also run with the correct Python version.
Using virtual environment you can install several python versions and more importantly you can install different modules version for each of those python revision (the main reason for running different python version is that some modules are not (yet) compatible with recent python releases).
You can check how to create/update and activate different virtual environment here.
Those env can use their own python version or share it, it is chosen at creation time (the python version you use when creating the env is the one that will be used any time you activate the env).
There is a default python version, namely python 2.6, on the GPU server with Linux OS. Now I want to install a new python version on the server from its source, namely python 2.7. I should not change the default python version since I am not the administrator and some reason. So what should I do?
You can install your new version of Python. It should be accessible with the python27 command (which may be a symbolic link).
Then you will just have to check that the python symbolic link still points to python26.
Doing this, python will keep on execute Python 2.6 while python27 will execute Python 2.7
You can use virtualenv, to execute your programm in an environment with python 2.7.
Install virtualenv and virtualenvwrapper (for comfotable use.)
mkvirtualenv -p <your-python-version> would then start a virtual environment where the desired python version is the default.
To build on Tryph's answer, you can install that new version to your home directory, then in a directory specified within your PATH (like in .bash_profile), you can point to that directory and within there create a sym-link that points to the new python.
For instance, if you have a bin folder in your home directory that is specified in the path
ln -s /bin/python ~/bin/python
I'm trying to understand how to create an isolated python environment using an alternative version of Python other than the default (in my case this is Python 2.7). virtualenv works on my system with Python 2.7, but I can't seem to create a virtual environment with a version of Python 3.
I tried to specify the Python version as outlined in this this post, like this:
$ virtualenv -p /usr/bin/python3.2
Can anyone explain how I can create a virtualenv with a specific version of Python? Thanks very much.
I believe you need to install python 3.2 to the system first. You're telling the virtualenvironment to use a version of python that doesn't exist anywhere so it cannot find it. This is the "does not exist"
Edit after more info was given in comment:
virtualenv -p /usr/local/bin/python3.2