I am starting to learn Python on my Mac and use VS Code. At the bottom left corner I can select the Python version. Apparently I have four versions installed.
I know that macOS already has one installed (I am assuming it is the one in /System/Library/..). I must have (accidentally) installed the other versions.
What is the safe/recommended way to set up Python3 on Mac for development (I only want/need one version 3.xx)?
Where should I install pip3?
I don't want to mess around with the system's python version, but delete everything unnecessary.
Thank you for advice!
Do not delete python2.7 as it may be used by system. Other than that, whenever you want to create any project in python is recommended to create a virtual environment, this way you can always separate what version will every project use and what libraries will you install for a specific library. More infor https://docs.python.org/3/tutorial/venv.html and how to uninstall python from mac https://osxuninstaller.com/uninstall-guides/properly-uninstall-python-mac/
Related
Currently, I feel like my Python ecosystem is out of whack. Several years ago, I had shared my computer with someone else, and now I am discovering that my machine has many versions of Python scattered about. From what I could find, starting from Macintosh HD:
anaconda
Applications/Python 3.6
Contains IDLE, Python Launcher, etc...
Library/Python/2.7/site-packages
This contains things like pip and wheel (I am unsure what this is)
Library/Frameworks/Python.framework/Versions
This contains two folders, 3.5 and 3.6.
System/Library/Frameworks/Python.framework/Versions
Inside here there are many alias folders, all which point to 2.7
Is all of this supposed to be normal? I am trying to run Python from the terminal, yet I have been getting messages such as ImportError: No module named site. pip has also not been working.
Is there a way for me to reset the Python on my machine to just that which comes with macOS? I feel like starting over from a blank slate would be helpful, since I want to get things setup in some type of comprehendible way (e.g. a way in which I've set things up so I know what is on my machine instead of many random things put on it by another).
This is very normal. you have a preinstalled python2.7 which comes with macOS and another one "Anaconda" which has installed manually. You first need to check what is your default python path (version) on your macOS, I mean if you are using anaconda or the default pre-installed python2.x. To remind you can check like below:
python --version
output (for me): Python 3.7.6
then if you want to change it to another version/or use another version under conda you can check these two answeres of mine.
How to add anaconda to PATH?
and here:
How to set the default python3 to python3.7?
It is normal that when you install a package for your default python version you don't expect it to be installed on the other one too. Normally it is better to install python2.x as an environment of conda and switch between two environments with "conda activate py2" and "conda deactivate" to go back to your default version. for each of them, you need to be in the environment and then use pip.
if pip is not working, it may need to be installed. On macOS, as you remember, you can use
brew update
brew install pip
or "easy-install" instead of brew (or whatever you use for installation).
I'm new to python. I installed python3.4 on OsX some time ago and now I installed python3.5 using the installer you can download from the site.
I noticed that in /Library/Frameworks/Python.framework/Versions/ I have both 3.4 and 3.5. I wasn't expecting that - I was expecting an upgrade where 3.5 replaced 3.4
So, if I run python3.5 and I try to import the packages I installed when using 3.4, they are not found. Furthermore if I use pip install to reinstall them, it says the packages are already installed, therefore I can see that it's pointing to the 3.4 version.
What I'm doing wrong? I supposed that installing the new python should upgrade my existing installation (bringing installed packages with it) rather than add a completely new install.
I'm not sure what to do now:
Should I keep every old version?
Should I manually change which pip
is used every time?
(is there a more streamlined update procedure
for next time?)
A lot of Python packages are 3rd party. The community is always moving forward and this may take some getting used to!
That said, my recommendation is to start using venv. It gives you (mostly) isolated Python virtual environments in which you can install whatever packages you like (via pip) without polluting the global installation. This also allows you to configure various virtual environments with varying packages and versions. It's really handy!
Link:
https://docs.python.org/3.4/library/venv.html
My mac has two versions of Python,
one installed in /Library/Frameworks/Python.framework/Versions/2.7/bin/pip
and the other /Library/Python/2.7/site-packages/
I have plenty of packages installed in the second directory. How do I move all those packages under the first python ?
Also How do I force my mac to use the first Python ?
Typically on a Mac you would expect to find:
/System/Library/Frameworks/Python.framework
/Library/Python
You should never touch the first, so the second exists to add local libraries.
What problem are you trying to solve by moving libraries?
I want to install Python 2.7 in two places at once on my Windows machine. For example, one in c:\python27 and another in c:\myproduct\python27. The official installer refuses to let me do this. If there is already an installation when I run the installer, it prompts me and asks if I want to Change, Repair or Remove the existing installation.
The TARGETDIR trick mentioned elsewhere on Stackoverflow doesn't work either - I get the same result if I type the following into a cmd window:
msiexec /i python-2.7.8.msi TARGETDIR=c:\myproduct\python27
So, is there anyway to install Python 2.7 twice on my Windows machine?
Reasons I want to do this are:
My product requires a 32-bit version of Python 2.7 to be installed because it uses ctypes to load a 32-bit DLL. If the user already has a 64-bit version of 2.7 installed, I don't think it is safe to install the 32-bit version over the top.
Relying on the users pre-installed Python 2.7 is bad because they might uninstall it later. That would stop my product from working.
It reduces the complexity of testing my product if I can make its installer always install a known version of Python. I can rely on it being in a known state.
I'd like to add the pyreadline module to the Python install that is part of my product. I suspect most users would rather that I did not mess around with their Python install.
When a user uninstalls my product, the Python it installed should also be removed. If I installed Python in the standard place the user might come to use it for other purposes and be surprised when it goes missing when they uninstall my product.
It appears that you should be able to install the 32 bit version of Python 2.7 in another folder when the 64-bit version is already installed. See How do I install Python 2.7.3 32 bit and 64 bit on Windows side by side for a description. However it may be tricky to automate this, you might have to get users to install it themselves.
I would suggest you ask your users to install Python 2.7 32-bit, and give them instructions on how to do it if they already have a conflicting 64-bit version. Then in your installation you:
Ensure that virtualenv is installed (and install it if it is not).
Create a virtualenv in your application folder.
That addresses everything except the second item on your list and I think you simply have to accept that no matter what you do, if someone removes a component that you need your application will fail. If they do that they can use the 'repair' option on your installer from which you can either reinstall the missing Python or tell them that's what they need to do.
To install virtualenv I would first install pip (see https://pip.pypa.io/en/latest/installing.html for instructions, you need to download get_pip.py and run it with the Python 2.7 interpreter), then just run pip install virtualenv.
Could there possibly be hidden files that I would need to find. Or do I have to re-install Python 2.7 if I want to work with it?
Thanks
Python installations on OS X generally go in separately and don't uninstall each other. Also, the convention is still for the executable python to refer to 2 and python3 to refer to 3, so they don't even really overlap.
Common locations you might have python include
/usr/bin/python (the system installed one, probably an osx specific 2.7.5)
/Library/Frameworks/Python.framework/Versions/... This is where the ones you install from Python.org go, separately for each version.
Your homebrew directory if you are using that
Which one runs when you type python (or python3) depends on your PATH environment variable.
python --version - will give you the version of the currently used python in environment variable PATH.
Nothing gets uninstalled. You have to just adjust the PATH variable according what you will be using.