Difficulty with using virtualenv and specific Python version? - 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

Related

Poetry activates wrong version of python macOS Big Sur

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

Can I ask a fundamental question about PyCharm base interpreter?

I am trying to use Python3 with PyCharm.
So I installed Python(3.10.1) and PyCharm(2021.3.1)
But when I try to create a new project, PyCharm want me to choose 'base interpreter'
and I don't know which one to choose.
Here is the list of base interpreter:
I want to know what is difference between /usr/bin/python3 and Library/Frameworks/Python.framework/Versions/3.10/bin/python3
Usually the files under /usr/bin/ are symlinks to actual files, which means they are not real files but links to the actual file.
To see it for yourself go to the /usr/bin/ directory with a terminal and execute:
ls -a
I suppose macOS already ships with python and by installing Python 3.10 you added that last element to the list, which is also linked by /bin/usr/python3.
I'd go with /usr/bin/python3, more portable.
You can choose the one interpreter for the version of python you want. In your terminal, you can run python --version or python3 --version and it will display the version of python, Like this:
$ python --version
Python 2.7.16
$ python3 --version
Python 3.9.1
Performing which python, will give you the path to that python binary, like the following (although, this is where we are leaving "Python" and talking more about the OS and PATH.
$ which python3
/usr/local/bin/python3
For the other versions of Python you have installed, you can check their version by doing something like:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 --version
You'll find that you may choose different versions of python for different projects. In the future, you may consider using venv to setup virtual python environments per project.
If you're just getting started with Python, I'd suggest sticking with the version you installed 3.10.1, and choosing that interpreter.
The base interpreter is the python.exe file that will run everything you do inside that project.
I would suggest you use Anaconda as a package manager and then create a virtual environment with the version of Python you need. Just follow these steps:
Getting started with Anaconda: https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
Choosing an Anaconda virtual environment with Pycharm:
https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
A package manager like Anaconda is very useful when working with Python.
Otherwise:
In your list you have many different versions of python. You should choose the base interpreter based on the python version you want to use.
Always select the latest version.
Since I use Python 3.9, it will be:
/usr/local/bin/python3.9
Based on your image, it would be:
/usr/local/bin/python3
The interpreter is what version your PyCharm project uses. It appears you have python2 and python3, so if you were writing in python2, you would use the python2 interpreter.
I would select the latest version, which in your case would be:
/usr/local/bin/python3

What determines a Python Flask project version?

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.

How I can delete python 3.4 if i use just anaconda python

I have a specific problem with python. I have on my ubuntu two versions python3.4 and python3.6(from anaconda). I want use just anaconda pythoncurrent version
But when i run script i have some problems with another python version
another version
How i can safe delete useless python 3.4.6?
sudo apt-get remove python3.4
anaconda should have set python3 as default python3.6 version
You should probably not delete Python3 from your system, even if you have Anaconda installed, since there might be system software that:
was not tested with subsequent versions of Python;
might struggle finding Python from Anaconda.
What you should do instead is configure your IDE / environment to run Python script with Anaconda, e.g. by setting your PATH variable to point to your anaconda/bin directory or similar.
If you are using PyCharm, as it seems from the screenshot, you could set up your project to run the Anaconda Python without modifying your other command-line settings.

Python - Virtualenv , python 3?

Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to have python 3.1.1 with subversion pygame to write my own stuff, and python 2.6 with normal stable pygame to use to run other things, like pygame games downloaded from pygame.org. Any help on how to accomplish that? Thanks.
OK I realized virtualenv is not what I'm looking for.
It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.
As I understand it you want to create different virtual environments with different Python versions?
This is very easy, and you only need virtualenv itself.
For, say, a Python 3:
$ virtualenv -p python3 p34env
(...)
New python executable in p34env/bin/python3.4
Also creating executable in p34env/bin/python
Installing setuptools, pip...done.
$ source p34env/bin/activate
(p34env)$ python -V
Python 3.4.2
(p34env)$ deactivate
$
You use the source command to activate the venv, and deactivate to - you guessed it - deactivate it. Notice the prompt changes to indicate the env.
For your system's standard version of Python you just skip the -p python3 argument, and you can use the argument to point to any version you want given a path.
The last argument is the name (p34env) and you can make as many as you like, just give them different names.
Your use case doesn't actually need virtualenv. You just need to install several different Python versions.
virtualenv is designed to create isolated environments of a Python environment. The trick to using it with multiple Python instances is to either install virtualenv into each of the Python versions you want to use it with, for example:
/usr/bin/easy_install-2.6 virtualenv
/usr/local/bin/easy_install virtualenv
sudo port install py26-virtualenv
or to invoke it with the intended Python version, for example:
/usr/bin/python2.6 virtualenv.py ENV
/usr/local/bin/python2.6 virtualenv.py ENV
/opt/local/bin/python2.5 virtualenv.py ENV
So, as such, it doesn't directly solve the problem (particularly acute on OS X) of which Python you want to work with. There are various ways to deal with that issue: use absolute paths to the intended Python (as in the above examples), define shell aliases, carefully manage the $PATH search order, among others.
At the moment, AFAIK, virtualenv is not supported with Python 3 because, among other things, setuptools (the magic behind easy_install) is not yet supported on Python 3, although there is work in progress towards a solution for that.
BTW, many people use Doug Hellman's virtualenvwrapper to simplify use of virtualenv.
For me virtualenv3 works very well. I also have pypi.python.org/pypi/distribute installed. That also works with the mentioned www.doughellmann.com/docs/virtualenvwrapper/ . I've only tested that on Linux though.
Not sure if I understood you correctly, but here goes :)
I don't know about OS X, but in Linux you can install both 2.6 and 3. Then you can either specify to use python25 or python3, or change the /usr/bin/python symlink to the version you want to use by default.

Categories