What is the difference between following commands:
python setup.py
and
python3 setup.py
What if I only have python3.6 installed? python and python3 would do the same thing?
Does it make difference only when I have different versions of python installed? If so, which version will be used with python setup.py?
There is no single correct answer here, but we can offer some common observations.
Some Linux distributions decided during the transition from Python 2 to Python 3 that python should always refer to Python 2, and the command to run Python 3 would be python3 with a 3 at the end. Now that Python 2 is becoming obsolete, this is being relaxed in some distros (i.e. now that they no longer ship Python 2 at all, the python command can be allowed to point to Python 3, too), but this is going to continue to exist in some form for some time.
See PEP 394
Debian-based Linux distros have a mechanism called update-alternatives which lets you define which version of python exactly will be linked to the system-wide standard /usr/bin/python. There is similarly a set of alternatives for /usr/bin/python3.
If you have manually installed Python 3 somewhere, what works depends on where it was installed and whether that location is in your PATH. A common arrangement is to have Python 3.14.159 somewhere like /opt/python-3.14.159/bin/python3.14.159 and then rely on users who want to use this to create a symlink or alias to this binary so that they can simply call it python (or python3, or obviously also e.g. shirley if they prefer that)
If you have an interactive alias, function, or personal shell script in your PATH with either of these names, obviously that overrides any system-wide setting, and could basically do anything at all, including but not limited to running a specific version of Python, ideally with all the command line arguments intact and correctly quoted (/path/to/system-wide/default/python "$#") but obviously with no guarantees of any sort.
On Windows, where many of these facilities don't exist, a common arrangement is for Python to be installed in C:\python3.14.159\bin\Python.exe; you then have to have C:\python3.14.159\bin in your PATH for the python command to work. The installer lets you install the package anywhere, but if you just go with the defaults, this is what you commonly end up with. Because of the cumbersomeness of Windows, the standard install also includes a command py which works around some of the rigidity of the platform to let you flexibly indicate which Python version exactly to run. There is usually not a separate python3 command, though users are of course free to create a CMD file with that name to run Python (or play a video of Rick Astley performing his biggest hit if they so prefer).
To help us help you debug questions about your installation, you will commonly need to tell us exactly what you installed where and how, and show us the value of your PATH. If you have relevant functions, aliases, or personal shell scripts which shadow the system-wide default ones, show them too. On Unix-based platforms, the type and command commands can be used to show what will be executed (many guidances will tell you to use which or whereis, but don't; now we have to guess which non-standard command you are using, and where it is going to look). The Windows command whereis provides roughly the same functionality.
Don't call me Shirley, please.
Yes, it will make difference if you have different versions of python installed.
This depends on the entries in on the PATH environment variable. Suppose you have two python installations, 2.7 and 3.8, now you have installed 2.7 before 3.8, and both were added to PATH, so when you type python, 2.7 interpreter launches. If you have done vice versa, then 3.8 would launch. You can type where python to determine location.
Also one thing is that there is a launcher named py, just type py -3.8 3.8 interpreter will launch and same on py -2.7
Related
I installed python using the command:
brew install python3
Now when I use 'which python', path is '/usr/bin/python' and when I use 'which python3' path is 'usr/local/bin/python3'
Shouldn't it fetch the same path ?
As python3, which is a version, still falls under python ?
I just am not clear as to why different paths are thrown. Please Explain!
Python is a program, that will take inputs, and interpret them. How will it interpret them ? Following a set of rules, written in a lot of files. Where are these files written ? Somewhere the program knows. And by default, the 2 paths you have are the paths it knows where to go.
Python2 and Python3 have different files, because even if they have the same base, they are not same and behave differently. Hence the 2 differents paths.
Though, through the years, people have come up with solution to avoid these "python version collisions" on computers : it is called a virtualenv.
Virtualenv is basically a script that will contain a whole new python (at the version you wish you install it), and, when you "activate" it, you will be able to use python, and have the version you wish to develop with. Doing this, everyone is able to only use python and still use different versions depending of the program you with to use.
Example : You have python on your system which writes Python 2.7.12 when you do python --version. If you instantiate your virtualenv (see the doc), and then use python --version again, you might see something else along the lines of Python 3.6.8. Your former computer, or other scripts, can still use the previous python version, and you new script can use the new one, without any conflicts.
just a quick question. I have Python 2.7 on my mac by default. I have also installed 3.4 and use it more than 2.7, but would like to upgrade to the new 3.5. Should I remove 3.4 and just lay down a new install of 3.5, or is there a way to just update it. All my searches just talk about upgrading from 2.7 to 3x. I am just concerned about messing one of the installs up. Any input would be greatly appreciated.
Cheers.
It is a lot safer to change your environment so that Python 3.5 is given preference over the default Python.
There are many ways to do this; if you do them all, it provides the maximum compatibility.
You can set these in your .bash_profile file, which is a hidden file in your home directory.
You can set the PATH environment variable so that Python 3.5 appears first in the search order; like this PATH='/path/to/your/python3.5/directory':$PATH
You can set a local alias in your shell, so that the python command points to Python 3.5, like this alias python="/path/to/the/file/python3.5"
Once you set the above, make sure you restart the terminal application.
If you download the installer form python.org; it will set these environment variables for you.
Also, if you use a utility like brew it will set the shell up correctly for you.
This will ensure that the shell environment will point to version of Python you want; however this does not affect applications that run on the desktop as most of them don't read the shell environment variables.
So, if you are using and IDE like PyCharm you'll still have to manually set the correct Python version for your projects.
This may seem like a lot of workaround, but on most Linux systems and even on OSX, Python is a core part of the system and it is used by some utilities, therefore it is always dangerous to rip and replace the version of Python that came with the operating system.
I'm developing python code that needs to run under 2.6+ and 3+ (I'm using python-modernize to make the code compatible). The code under development has no C extensions or 3rd party Python package dependencies except for pexpect3.3 which is used solely for testing.
How can I install python 3.4 on OS X 10.9.5 for the sole purpose of testing the code under development, i.e. I want to be able to run 'python3 myscript.py' but otherwise be sure that python3 will not be invoked as 'python' and without compromising the Enthought Canopy python2.7 environment that's my day-to-day standard for all other python work?
I'm aware, from other SO answers, e.g. Python 3 in Enthought Canopy, that Canopy doesn't support 3.x, so I'm not trying to make it work in that environment. I just need 3.x for some command line testing.
Should be no problem at all. Just ensure that your Python 3 is not set at installation to be the default Python in Terminal (i.e. not on PATH). And if for some reason it does end up in PATH, hunt it down and remove it from your ~/.bashrc, ~/.profile, ~/.bash_profile. I prefer to have no default python and only to set it into PATH as needed using an alias.
I'm a newbie programmer just installing Python 3.2, but I know I also have an older version of Python on my machine. in fact, I think Macbook comes with it installed. Do I have to worry about having different versions on my computer when I try to start learning Python?
For the most part, you don't have to worry about conflicts with system Python. In fact it is recommended to install a different Python version instead of working with system Python. Also consider using virtualenv and virtualenvwrapper to maintain any dependencies for each project easily without conflicts.
It really depends what OS you're talking about. I'm assuming you're talking about a Mac, since you mentioned Macbook.
Macs come with 2.5 and 2.6 installed as far as I'm aware. At least mine has both those versions, and I've only installed 2.7 manually.
You can check which version of python is the current 'system' python by doing the following in terminal:
// check the version of system python
python --version
// tells you where the system version of python is on your PATH
which python
On *nix type Operating Systems, like your Mac, applications aren't really 'installed', like they are in Windows (eliding details). Instead, application files are placed in various different parts of the file system. Python, for example, is placed into the following directory (by default) when installing 2.7:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Since this directory isn't on the system PATH, this version of python won't be used when simply calling python from the command line. The system will search all the folders in the PATH environment variable for an executable file called python. It will usually find it in /usr/bin/ or something similar.
To make a new version of Python the 'system' python, you have a couple of options:
Modify your .bash_profile, and prepend the path to your new python to the PATH environment variable.
symlink the new version of python to a directory already on your PATH like /usr/bin/
Be aware that Mac python installers can modify your .bash_profile (in your home directory), to force the new version to be the default system version. This is what my bash_profile shows:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
You can happily run multiple versions of python on the same system. A particular version is usually the default though, and that's whatever executable python file is found on the PATH first.
If you want to use a different version at any particular point in time, you can:
/path/to/python/2.4/python some_script.py
/path/to/python/2.7/python some_script.py
/path/to/python/3.2/python some_script.py
That will execute the script some_script.py under 3 different versions of python. Of course, you need to make sure that the /path/to/python is correct.
So yes, you need to be mindful about what version of python you are going to be using, hopefully this will guide you into understanding how applications are installed and which version of an application is launched by default when you don't provide a path.
Yes, 3.x Python syntax is not backward-compatible with 2.x. So if you learn Python 3.x you might not be able to port your knowledge to Python 2.x.
Moreover you should choose if you want to learn 3.x or 2.x. 2.x is far more widespread than 3.x, but 3.x is where Python is heading. No more innovation will happen in 2.x, and in mid-term most frameworks will be ported to 3.x (right now there are some notable exceptions)
Hope that helps!
In general, you should be fine. Since the Mac is BSD-based, it should maintain the "python" command as pointing to the version that your system requires, which is usually an older version like 2.5. You may have to use a command like python3 to run your Python 3 programs, but other than that it should be transparent to you.
As you learn and become more advanced, you can begin using the virtualenv system to maintain separate Python installations for multiple projects.
Python version with different major or minor version numbers can be installed in parallel. For example, you can have 2.4, 2.5, 2.6, 2.7 and 3.1 on the same machine. However, you can't have versions with the same major and minor number installed at the same time (at least, not without tricks), so you can't have 2.5.2 and 2.5.4 at the same time.
Note that you will have to install any third-party libraries once for every Python version.
It is very well possible to have multiple versions of python on your machine. Just make sure, that if you call python in your console it uses the python you want it to use. Same goes for your IDE.
Regarding the version: It is always nice to have the latest version on board (in python however there are compatibility issues to take into account) , since there might be features you want to use, that are only available with a certain version and upwards. Since this is sometimes tricky to find out, especially if you are new to the field, going with the latest version might be how you should proceed.
Be careful before installing new version of python.
Python has no backward compatibility.
Scripts written for python 2.7.* won't work on python 3
For example,
print "Hello" will work on python 2.7 but not on version3
I'm looking for a way to let multiple Python programs coexist on the same Windows machine.
Here's the problem: suppose program A needs Python 2.5, B needs 2.6, C needs 3, and each of them needs its own version of Qt, Wx or whatever other modules or whatever.
Trying to install all these dependencies on the same machine will break things, e.g. you can install different versions of Python side-by-side but only one of them can have the .py file association, so if you give that to Python 2.5 then B and C won't work, etc.
The ideal state of affairs would be if program A could live in C:\A along with its own Python interpreter, Qt/Wx/MySQL driver/whatever and never touch anything outside that directory, ditto for B and C.
Is there any way to accomplish this, other than going the full virtual box route?
edit: I tried the batch file solution, but it doesn't work. That is, it works on simple test scripts but e.g. OpenRPG fails at some point in its loading process if its required version of Python doesn't own the file association.
VirtualEnv.
virtualenv is a tool to create
isolated Python environments.
The basic problem being addressed is
one of dependencies and versions, and
indirectly permissions. Imagine you
have an application that needs version
1 of LibFoo, but another application
requires version 2. How can you use
both these applications? If you
install everything into
/usr/lib/python2.4/site-packages (or
whatever your platform's standard
location is), it's easy to end up in a
situation where you unintentionally
upgrade an application that shouldn't
be upgraded.
See previous answer here.
The other tool you should look at is pip which is great for installing particular versions of a library into a virtual environment. If you need to run v 1.0 of a library in python v 2.x for one application and 1.1 of the same library in python v 2.x, for example, you will need virtualenv plus a means of installing a particular version in that environment. Virtualenv + pip is your best choice.
Use batch files to run scripts, write in notepad for example:
c:\python26\python.exe C:\Script_B\B.py
and save it as runB.bat (or anything .bat). It will run with interpreter in c:\python26\python.exe file specified after a whitespace.
One solution would be to craft a batch file that invokes the correct interpreter for a given application. THis way, you can install additional interpreters in separate folders.
Probably not perfect but it works.
Have you considered compiling them to EXEs? Once you do that, all you have to do is call the EXE, for which the machine does not require python to be installed. All the required modules etc are packaged with the distribution when you compile.
write a python script that mimics the way unix shells handle scirpts -- look at the first line and see if it matches #!(name-of-shell). Then have your python script exec that interpreter and feed it the rest of its arguments.
Then, associate .py with your script.
It looks like the best solution is a batch file that sets the file association before running the appropriate version of Python, as mentioned in the comments to one of the answers here: how to run both python 2.6 and 3.0 on the same windows XP box?