I am new to linux and somewhat new to python. I am trying to follow a tutorial that is using python 3.9, however I was using python 3.4.2. Once I realized I was out of date I downloaded python 3.9.0 and when I run
python -V
in the terminal is stats Python 3.9.0, but the python shell is still running 3.4.2
I am not sure how to change the python version for the current project, or start a new project in python 3.9 when I click on python 3 idle in the menu.
It still states python 3.4.2
So how do I get the python idle to run python 3.9.0 and not 3.4.2?
I have 2 python versions in my PC as well (2 and 3), what I usually do is to add an alias in ~/.bashrc, and update it when needed, example alias python="python3" or if python2 is needed just alias python="python2"
Related
I just downloaded VS Code this week and have run into a problem accessing different versions of Python.
When I run a python file called set.py from the command line via python set.py, VS Code is accessing the Python 2.7 version that came with my MacBook. When I run python --version it confirms that VS Code is using Python 2.7.16.
However, when I right-click in the editor and select Run Python File In Terminal it appears to be using the updated version 3.9.4. I have tested this by using an fstring (available in 3.9 but not in 2.7). The fstring code throws an error in the first method but clears fine in the second. Here's my code:
s = set()
s.add(1)
s.add(2)
s.add(3)
print(f"The set has {len(s)} elements.")
And here's my terminal with the results of both methods shown.
Does anyone know what exactly is going on, and how I can execute python files via the command line?
TIA
Specs:
I am running VS Code 1.55.2 and have downloaded Python 3.9.4 to my MacBook Pro running Big Sur 11.2.3
I have also downloaded the Python extension in VS Code and have selected Python 3.9.4 64-bit as my interpreter.
On macOS and linux, you will have a system python that tends to be old and you don't want to touch it because there are system utilities that rely on it.
You will also have the user-installed python, which here is python 3.9.
If the system python is python2, then the command python will typically invoke python2 and the command python3 will invoke python3.
In VS Code on Windows, when you launch a terminal window from the IDE, it will activate the interpreter/environment you've chosen for your project (typically by CTRL-SHIFT-P and then Python: Select Interpreter). I've noticed on Linux that it will not do this (and this may also be the case on macOS), so if I want to use a specific version or environment, I need to specify it or activate it as my first command in the terminal with something like conda activate myenvname.
Unlike in the terminal, VS Code will use the selected interpreter if you run code directly from the IDE (using Run Without Debugging or Control + F5).
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).
I've tried to upgrade Python version to 3.8 and when I want to add the interpreter in PyCharm I get the following:
I have no idea as to why this happens. When I type python -V in the command prompt I get Python 3.8.3.
EDIT: Python version actually is updated but PyCharm still marks the code as if the version is 3.
Without root access, how do I change the default Python from 3.5 to 2.7 for my specific user? Would like to know how to run Python scripts with Python 2 as well.
If I start up Python by running simply python then it runs 3.5.2. I have to specifically run python2 at the terminal prompt to get a version of python2 up.
If I run which python, then /data/apps/anaconda3/bin/python gets returned and I believe Python 2.7 is under /usr/bin/python.
This is on CentOS if that helps clarify anything
You can add
alias python=python2.7
to your .bashrc file in home folder
If you are looking to change the python interpreter in anaconda from 3.5 to 2.7 for the user, try the command conda install python=2.7
I am trying to uninstall Python 3.2 and go back to 2.7, I do not have much experience with Python, and as I am learning now, it is becoming increasingly difficult to manage all of the work arounds needed to work with the newest version, and I require 2.7 for a college course.
I tried using the Windows 7 add/remove programs to uninstall Python 3.2 and 3.1, and then I downloaded and installed the 64 bit Python 2.7, but I was unable to open any of my already existing .py files with the executable in the Python27 folder.
When I click to open any .py file, I don't see any error messages but Windows asks what program to open the file with, and if I choose python.exe or pythonw.exe in C:\Python 27 I get a console Window appearing briefly and then closing. The "edit with IDLE" option in the context menu is gone, and if I try to edit with IdleX, I get another "what application to open with" window.
Instead of removing python 3.2, you can use both of python 2 and 3 in the same time. You just need to specify which version you want to use.
When in CMD, you can see the available versions installed on your windows. If it doesn't appear there, you need to install it.
py -0 # Prints out the versions you can use.
You can specify which python version you want to use.
For example in Windows 10, I use the code below for python 3.
py -3 fileName.py # runs using python 3
For python 2, you can use the code below after installing it.
py -2 fileName.py # Runs using python 2
Here is more information about installing more versions of Python in the same time.
How to install both Python 2.x and Python 3.x in Windows 7
Python 3.x is not backward compatible with Python 2.x, which was the purpose of the release. To clean up Python2.x without worrying about backward compatibility.
You can have as many python installations as you want on your computer, as they do not interact with each other. The python installations have it's own folder, with it's own idle, modules, launcher, ect.
You could install PyCharm or another Python IDE. It allows you to change which version of python you use to interpret your code. This will also identify syntax errors as you are writing and will notify you of them - in case you have code that works in 3.x but not 2.x.
Ecker00 is right, installing 2.7 in a separate directory gives you access to 2.7 while still having 3.x on your computer. You will have to re-install libraries with the Python27 folder's pip in order to use them in 2.7 though.
You can also change your python version using anaconda:
conda install python=3.5.0
or maybe
conda install python=2.7.8
or whatever you want.
Use the following steps to get it fixed.
//Check current Python pointer
ls -l python
//Check available Python versions
ls -l python*
//Unlink current python version
sudo unlink python
//Select required python version and lin to python command
sudo ln -s /usr/bin/python2.7 python
//Confirm change in pointer
ls -l python