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.
Related
I'm using a Mac and this is a screenshot of the files in my /usr/local/bin
This is a screenshot of the files in /opt/homebrew/opt
I'm wondering why I have this amount of Pythons on my Mac. I feel like it's cluttered and confusing. I am also aware that the Macs have a built in Python so just to be clear, I'm not trying to get rid of that. What makes this confusing is that I don't know which Python is in use to update. Typing python3 --version returns 3.9.5 when the latest is 3.9.7. Furthermore, I thought that I had updated my Python through the official website, but found out that I just updated the IDLE that downloads when you install Python from Python.org.
Although a seemingly trivial question, any feedback and knowledge would be appreciated!
The command which python at the terminal will tell you the path of the command python. It might be a symlink so use ls -l on that path to check.
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
I just dealt with the worse bug in my entire 3 years of computer programming! It turns out that because I wanted to work with the natural language toolkit I had to install python 3.5 even though I'm using python 3.6. So I downloaded 3.5 and now my terminal is using python 3.5 by default and I can't get it back to 3.6. Because I was using python 3.5 which does not automatically order dictionaries it was throwing my program off because it relies on ordered dictionaries. It took me 4 hours to figure that out.
You want to use virtualenv and/or virtualenvwrapper. This is a utility that allows you to use multiple different environments, with different Python versions, different pip packages installed, etc.
To find the 3.5 version, run which python in your terminal to find the path to the python executable; then look at your PATH environment, and see where the location of that Python is on your PATH. Then you need to find out where that path is getting added; this will depend on your OS/Shell.
Tough times for sure, sorry to hear that.
I use pyenv to manage the different python versions on my system. This allows you to create virtual environments using whichever version you want.
EDIT to address comments.
I totally understand that setting up virtualenv or something like pyenv is not simple. However, it is unfortunately the easiest way to deal with (and avoid) situations like this. There are two essential concepts that are important here:
1) Isolation - Virtualenv takes care of this. When you install dependencies in a virtual environment, they will not affect your other environments or system python installation.
2) Multiple Python Versions - In your case, you needed to use a module that did not support 3.6. Instead of creating a virtual environment using python 3.5, you accidentally messed up your system installation of 3.6. Recovering from these types of misconfigurations can be difficult, and it is often easier to simply prevent it in the first place.
Again, I completely understand that this might be complicated, I remember thinking the same thing, but it is less complicated than troubleshooting the misconfigurations that can occur without this tooling.
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 use python2.7 mostly, but I wanted to use python3.3 for a specific task. I referred to the existing questions Python 3x and python 2.x The solution suggested there did not work for me. I have couple of questions based on the issues I have been facing.
Adding python3.3 to the path variable.Some post(add python27_path) suggested to copy the file and rename it. I tried renaming C:\Python3.3\python.exe to C:\Python3.3\python3.exe. But this did not work for me.
Adding libraries to PYTHONPATH: Had added C:\Python33\Lib\site-packages to the PYTHONPATH. When I ran the code with Python3.3, it was using libraries of python2.7. Is it possible for the libraries to coexist on the same machine and if I call python2.7 it should look only for its modules?
Those lovely people over at python have come up with the perfect solution for you as a part of python 3.3 - a launcher for windows that works all this out for you take a look about half way down this page.
The other option is to have a switcher script which changes your path and pythonpath variables for you.
Well, you can explicitly specify which version of python to use by making sure that you add the appropriate python location to the beginning of the path before you invoke the python command. Something like this.
Let's assume that you PATH variable in Windows is : c:\windows\system32;c:\python27\;...
Execute your python scripts using 3.3 this way :
SET PATH = "c:\python33\";%PATH%
python yourscript.py
Execute your python scripts using 2.7 this way :
SET PATH = "c:\python27\";%PATH%
python yourscript.py
This is a good way to execute scripts without having to install too many third party software products. A simple BAT file can then solve your requirement.
The variant
SET PATH = "c:\python27\";%PATH%
is invalid. You should use
SET PATH=C:\python33\;%PATH%
On windows, if you already added both versions of python to the PATH, you can use:
py -2.7
or
py -3.6