I have python 2.7.6 and python 3.4 installed on windows 7 machine.
When I open command prompt for windows and type python, python 2.7.6 starts by default.
I have a python script which I want to compile (or interpret officially speaking) using python 3.4.
Is there a command to use python 3.4 from c:/ prompt? or make 3.4 the default python interpreter?
thanks
Recent versions of Python for Windows install a script called py that will do what you want.
You can either do py -3 script.py to explicitly tell the launcher that you want to use Python 3
or put something containing "python3" on the first line in a comment (ideally #!/usr/bin/env python3 for compatibility with other systems...) and just run script.py; the installer associates *.py files with the launcher, which in turn determines the version to run by looking at the first line of the script.
This mechanism is described in PEP 397.
To make sure that you are always running python 3, you can modify your Windows PATH Environment variable to include the python 3 directory, and remove the python 2 directory.
Related
I've installed Python 3.9 on my Mac, and used Python: Select Interpreter in VS Code to change to it. VS Code is telling me its using Python 3.9 in the status bar, but when I type "python -V" in the terminal it tells me its using Python 2.7. See attached screenshot.
screenshot
Python 2.7 comes pre-installed in MacOS. The command python refers to it.
If you want to use python 3.x instead, you have to use python3 (as in python3 --version)
If you would like to invoke python3 even when you type python, you can add an alias to your bashrc or your zshrc (depending on which you use) such as
alias python="python3"
I am trying to debug c++ program. I source a script from gdb which is written in python 2.7 and it supposed to recursively print object values. Python 3 was already installed in my linux machine. So I installed python2.7 and now my machine has both python 2 and python 3. Now when I run the source command in gdb environment, it still uses python 3 and the script does not run. How do I set it to run on python 2.
I have tried setting alias python=/usr/bin/python2.7 in .bashrc.
I do not want to uninstall python 3 as other services depends on it.
Expect gdb to use python 2 while doing source python script.
Python 3 was already installed in my linux machine. So I installed python2.7
Installing Python 2.7 will have to effect on GDB: what matters is which version of Python was your copy of GDB compiled and linked against.
To get a GDB built against Python 2.7, you will most likely have to build GDB yourself (use --with-python=... when configuring your GDB).
I am using python 2.7.10 with Anaconda 2.3.0, and I use the Anaconda IDE with sublime text 3 (is it a different Anaconda??).
However, for a course I'm taking we're required to use python 3.5.
I would like to keep python 2.7 on my computer, though. What would be the easiest way to have both versions coexist and be easily accessible through the terminal (through sublime would be great, but I'd be happy with managing it on the terminal for now)?
Also, I typically run conda update conda, and conda update anaconda on a regular basis to keep everything up to date. How would that work with different versions of python installed?
This sort of thing is actually what Anaconda is built for. Although the default Python version depends on the installer you used, Anaconda supports both versions. The easiest way is to create a new virtual environment. From the following link, use this conda command to build a Python 3 environment:
conda create -n py35 python=3.5 anaconda
That is if you need Python 3.5 with all the anaconda packages. You can either leave that blank if you just want a vanilla version of Python 3.5, or specify individual packages.
Once you do this, Python 3.5 will be available with the console command py35. You should definitely read the following link about how to manage environments. Really, you should read that whole tutorial.
I am attempting to use spatialite with both Python 2 and 3 on Windows 7.
Rather than try to patch pyspatialite for Python 3, I decided to use the load_extension approach with sqlite3 Python built-in package, similar to how is done here: Sqlite load_extension fail for spatialite in Python and here: Use spatialite extension for SQLite on Windows.
But, on the official (C)Python 2.7 installer, the load_extension was disabled for an issue related to MacOS. This is not with the counterpart for Python 3.4. Furthermore, both installers are built without SQLITE_ENABLE_RTREE=1 (that I'd also like to have).
At first, for Python 2.7, a workaround was to build pysqlite tweaking the setup files to have both R*Tree and extensions. This way does not work for Python 3, since it does not seem to be supported by the current setup.py. From my understand, this is because the package moved to the core Python repository: https://github.com/ghaering/pysqlite/issues/72#issuecomment-94319589
My current solution was to re-build both Python 2.7 and 3.4 with required settings for sqlite3 package. It worked, and I was able to load spatialite as an extension and to create R*Tree.
Does it exist an alternative simpler solution? Did somebody find an alternative solution by working on the setup.py of pyspatialite or pysqlite?
There is an easier solution that I just got working. I'm running Windows 10 with Python 3.5 using the default sqlite3 package, and Spatialite 4.3.
This is how I did it:
Let start with a simple program that loads the spatialite extension (which we haven't installed yet):
import sqlite3
with sqlite3.connect(":memory:") as conn:
conn.enable_load_extension(True)
conn.load_extension("mod_spatialite")
If you run this program you get:
C:\> python test.py
Traceback (most recent call last):
File "test2.py", line 5, in <module>
conn.load_extension("mod_spatialite")
sqlite3.OperationalError: The specified module could not be found.
That means it can't find the mod_spatialite extension. Let's download it from the Spatialite website. At the bottom there's links to "MS Windows binaries". I picked the x86 version, current stable version, and downloaded the file called mod_spatialite-4.3.0a-win-x86.7z. Unzipping that file to the same directory your test.py file is in, and running the program now produces no errors (ie. it works!):
C:\> python test.py
C:\>
I have just installed Ubuntu 12.04 which comes with Python 2.7. I have installed Python 3.3, so now I have both versions. For example, if I type python in the terminal I get version 2.7 and if I type python3.3 I get that version.
I don't see why I would need 2 versions (?) so how do I uninstall Python 2.7? And if I do so, will the "python" command then point to Python 3.3?
VERY IMPORTANT EDIT
Removing an older version of python may be very dangerous and can cause trouble in your whole system! For your case instead of removing the older python you can simply use an alias in your terminal, so that when you type python it opens python3.3;
Here is the procedure, add this line:
alias python=python3
Into~/.bash_aliases or: ~/.bashrc
Btw I guess If you ask this question in AskUbuntu you may have a quicker/better response!
You should never remove the builtin Python in your Ubuntu distribution. Bad things will happen if you do.
It is highly recommended to use virtualenv to install other Python environments.
Here's a good Stackoverflow question that demonstrates how:
Is it possible to install another version of Python to Virtualenv?