Python on Mac woes - python

I want to use the latest Python, 3.6. Installed that, fine.
Now I want to install Beautiful Soup to try some web scraping. After several web searches I found how to operate easy_install. But not on 3.6.
How do I tell the system that everything I want to do is to refer to 3.6?

If you install things system-wide you'd need to use the binaries that come with it, for example pip or python binary that is installed together not the default version. It all depends on your system path which one is picked up but it can get complicated with multiple versions.
I'd advise you install pyenv and use that to change python versions. It's on brew. You'll be able to install different versions easily using pyenv install 3.6.1 and change to different versions using pyenv shell 3.6.1 for example. Shim executables will know which versions of binaries to use. Just remember to run pyenv rehash when you install a new binary so it is registered by peynv.

Related

Trouble switching between two Python 3.x versions

I am new to Python, and I have been trying to follow their online tutorial which relies on the current version Python 3.10 being installed. I've tried several solutions to upgrade from 3.9.12 to the most recent 3.10.4, but all modes of calling python default to 3.9.12.
I am using a Mac, and I first tried to use homebrew with "brew install", but I've read that homebrew hasn't yet switched from 3.9. I next tried to update through Anaconda by running "conda install python=3.10.4", but Anaconda Navigator is still using 3.9 for all environments.
I know that 3.10.4 has been installed on my computer; however, I lack the skillset right now to find out where it is and how I can change the default to calling from wherever 3.10 is right now.
I would appreciate help with this! Please let me know ways that I could change the default version of python being called (whether in Anaconda or just through access via terminal). I've found a lot of resources for changing between python2 and python3, but not much for switching between python3 versions.
Thanks!!!!
I'm not familiar with Conda, but I would recommend pyenv to solve this exact problem. pyenv helps you manage different Python versions, and set different versions per project, or per shell. This is very useful when working on different project or testing different distributions.
You basically install with brew like
brew update
brew install pyenv
Then set your environment, with these steps. (It will depend on your shell).
Then you can start installing different versions like:
pyenv install 3.10.4
pyenv install 3.7.3
...
With versions installed, you can set the global, local and shell versions.
# This will set the default global python3 to the specified version
pyenv global 3.10.4
# This will set the default interpreter in a specific project/folder
pyenv local 3.7.3
# This will set the current shell to a specific version
pyenv shell 3.9.6
IMHO it's the easiest way to work with different versions.
Here's the link to the official repo, and I would also suggest this article from RealPython, that's explains it all in much greater detail.

why are there multiple pip versions in my Python scripts folder?

I have a variety of different Python versions installed on my Windows system- a 2.7 version, a 3.5 version, and a 3.6 version (there are a bunch of different packages that only work with one version, or are too buggy in the 3.6 version, etc.).
Long story short, I'm trying to keep my all my pips and python.exes in order. I've added my C:/Python35 and C:/Python36 and their Scripts folders to my path, but I also want to make sure that I am using the right pip from my command line (for example, I don't want to pip install pyinstaller to the 3.6 version, since Python 3.6 doesn't play well with pyinstaller as of right now.
I see that inside my Python3x/Scripts/ folder, there are three different pips available: pip, pip3.5, and pip3.
So whenever I want to install a module for 3.5, I plan to issue the following command pip3.5 install package_name. Whenever I want to install something for 3.6, I'd use pip or pip3. Seems like a decent enough plan to me.
However, can anyone confirm if the three pips are all the same executable? If so, I'd like to delete pip and pip3 so that I don't accidentally confuse it with my Python 3.6 pip- is this acceptable practice or am I missing something? This SO post provides some insights but doesn't explain why there's multiple pips in the same folder. Also, why are three separate pips provided? Is it simply for convenience from the command line?
Within the same python installation all the different pip files you find should be the same executable, there is the multiple versions simply to help keep everything in order if there are multiple installs of python on a single computer.
I personally only have the main version of python I use for development set to my PATH variable on my windows laptop and then if I need to do anything to a different python I instead link directly to the necessary file with something like C:\Python36\Scripts\pip3 install natsort but that is simply personal preference and my way of organizing.
If you do have them all on path you can then simply call out pip3.6 install <package name> or whatever python version you are using
The difference between them is that each one install the package in its own folder, for example if i type pip install Django, it will be placed for python 2 version, but is a little bit complex when you have multiple version of python3 like you showed, the solution: Don't delete the files and makevirtualenv when you're working, that avoid problems.
This prevents dependency issues with different versions of Python. You also check out virtualenvwrapper which is a convenient way to manage your virtual environments
If you want to manage the version with virtualenv
virtualenv python2_project -p usr/bin/python2.7
virtualenv p35_project -p usr/bin/python3.5
virtualenv p3x_project -p usr/bin/python3.x

How can I install multiple copies of Python 2.7 on the same machine?

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.

pip installation on different python version

I encounter a problem with pip installation on linux. I've python 2.7 and 3.4, also Django in 1.7 installed. Currently I'm working on a project which uses different versions and I'm unable to install packages trough pip on python 2.7. Everything goes to directory of 3.4.
Is there any way to "force" pip to install packages in concrete version of python?
The usual, and recommended by most users, way of working with Django is to use a separate, virtual environment per project.
Use virtualenv to set up your Python 2.x environment and venv for Python 3.x. Both will install their own, local version of pip. Google lists lots of tutorials if you need help beyond the documentation.

What is the most compatible way to install python modules on a Mac?

I'm starting to learn python and loving it. I work on a Mac mainly as well as Linux. I'm finding that on Linux (Ubuntu 9.04 mostly) when I install a python module using apt-get it works fine. I can import it with no trouble.
On the Mac, I'm used to using Macports to install all the Unixy stuff. However, I'm finding that most of the python modules I install with it are not being seen by python. I've spent some time playing around with PATH settings and using python_select . Nothing has really worked and at this point I'm not really understanding, instead I'm just poking around.
I get the impression that Macports isn't universally loved for managing python modules. I'd like to start fresh using a more "accepted" (if that's the right word) approach.
So, I was wondering, what is the method that Mac python developers use to manage their modules?
Bonus questions:
Do you use Apple's python, or some other version?
Do you compile everything from source or is there a package manger that works well (Fink?).
The most popular way to manage python packages (if you're not using your system package manager) is to use setuptools and easy_install. It is probably already installed on your system. Use it like this:
easy_install django
easy_install uses the Python Package Index which is an amazing resource for python developers. Have a look around to see what packages are available.
A better option is pip, which is gaining traction, as it attempts to fix a lot of the problems associated with easy_install. Pip uses the same package repository as easy_install, it just works better. Really the only time use need to use easy_install is for this command:
easy_install pip
After that, use:
pip install django
At some point you will probably want to learn a bit about virtualenv. If you do a lot of python development on projects with conflicting package requirements, virtualenv is a godsend. It will allow you to have completely different versions of various packages, and switch between them easily depending your needs.
Regarding which python to use, sticking with Apple's python will give you the least headaches, but If you need a newer version (Leopard is 2.5.1 I believe), I would go with the macports python 2.6.
Your question is already three years old and there are some details not covered in other answers:
Most people I know use HomeBrew or MacPorts, I prefer MacPorts because of its clean cut of what is a default Mac OS X environment and my development setup. Just move out your /opt folder and test your packages with a normal user Python environment
MacPorts is only portable within Mac, but with easy_install or pip you will learn how to setup your environment in any platform (Win/Mac/Linux/Bsd...). Furthermore it will always be more up to date and with more packages
I personally let MacPorts handle my Python modules to keep everything updated. Like any other high level package manager (ie: apt-get) it is much better for the heavy lifting of modules with lots of binary dependencies. There is no way I would build my Qt bindings (PySide) with easy_install or pip. Qt is huge and takes a lot to compile. As soon as you want a Python package that needs a library used by non Python programs, try to avoid easy_install or pip
At some point you will find that there are some packages missing within MacPorts. I do not believe that MacPorts will ever give you the whole CheeseShop. For example, recently I needed the Elixir module, but MacPorts only offers py25-elixir and py26-elixir, no py27 version. In cases like these you have:
pip-2.7 install --user elixir
( make sure you always type pip-(version) )
That will build an extra Python library in your home dir. Yes, Python will work with more than one library location: one controlled by MacPorts and a user local one for everything missing within MacPorts.
Now notice that I favor pip over easy_install. There is a good reason you should avoid setuptools and easy_install. Here is a good explanation and I try to keep away from them. One very useful feature of pip is giving you a list of all the modules (along their versions) that you installed with MacPorts, easy_install and pip itself:
pip-2.7 freeze
If you already started using easy_install, don't worry, pip can recognize everything done already by easy_install and even upgrade the packages installed with it.
If you are a developer keep an eye on virtualenv for controlling different setups and combinations of module versions. Other answers mention it already, what is not mentioned so far is the Tox module, a tool for testing that your package installs correctly with different Python versions.
Although I usually do not have version conflicts, I like to have virtualenv to set up a clean environment and get a clear view of my packages dependencies. That way I never forget any dependencies in my setup.py
If you go for MacPorts be aware that multiple versions of the same package are not selected anymore like the old Debian style with an extra python_select package (it is still there for compatibility). Now you have the select command to choose which Python version will be used (you can even select the Apple installed ones):
$ port select python
Available versions for python:
none
python25-apple
python26-apple
python27 (active)
python27-apple
python32
$ port select python python32
Add tox on top of it and your programs should be really portable
Please see Python OS X development environment. The best way is to use MacPorts. Download and install MacPorts, then install Python via MacPorts by typing the following commands in the Terminal:
sudo port install python26 python_select
sudo port select --set python python26
OR
sudo port install python30 python_select
sudo port select --set python python30
Use the first set of commands to install Python 2.6 and the second set to install Python 3.0. Then use:
sudo port install py26-packagename
OR
sudo port install py30-packagename
In the above commands, replace packagename with the name of the package, for example:
sudo port install py26-setuptools
These commands will automatically install the package (and its dependencies) for the given Python version.
For a full list of available packages for Python, type:
port list | grep py26-
OR
port list | grep py30-
Which command you use depends on which version of Python you chose to install.
I use MacPorts to install Python and any third-party modules tracked by MacPorts into /opt/local, and I install any manually installed modules (those not in the MacPorts repository) into /usr/local, and this has never caused any problems. I think you may be confused as to the use of certain MacPorts scripts and environment variables.
MacPorts python_select is used to select the "current" version of Python, but it has nothing to do with modules. This allows you to, e.g., install both Python 2.5 and Python 2.6 using MacPorts, and switch between installs.
The $PATH environment variables does not affect what Python modules are loaded. $PYTHONPATH is what you are looking for. $PYTHONPATH should point to directories containing Python modules you want to load. In my case, my $PYTHONPATH variable contains /usr/local/lib/python26/site-packages. If you use MacPorts' Python, it sets up the other proper directories for you, so you only need to add additional paths to $PYTHONPATH. But again, $PATH isn't used at all when Python searches for modules you have installed.
$PATH is used to find executables, so if you install MacPorts' Python, make sure /opt/local/bin is in your $PATH.
There's nothing wrong with using a MacPorts Python installation. If you are installing python modules from MacPorts but then not seeing them, that likely means you are not invoking the MacPorts python you installed to. In a terminal shell, you can use absolute paths to invoke the various Pythons that may be installed. For example:
$ /usr/bin/python2.5 # Apple-supplied 2.5 (Leopard)
$ /opt/local/bin/python2.5 # MacPorts 2.5
$ /opt/local/bin/python2.6 # MacPorts 2.6
$ /usr/local/bin/python2.6 # python.org (MacPython) 2.6
$ /usr/local/bin/python3.1 # python.org (MacPython) 3.1
To get the right python by default requires ensuring your shell $PATH is set properly to ensure that the right executable is found first. Another solution is to define shell aliases to the various pythons.
A python.org (MacPython) installation is fine, too, as others have suggested. easy_install can help but, again, because each Python instance may have its own easy_install command, make sure you are invoking the right easy_install.
If you use Python from MacPorts, it has it's own easy_install located at: /opt/local/bin/easy_install-2.6 (for py26, that is). It's not the same one as simply calling easy_install directly, even if you used python_select to change your default python command.
Have you looked into easy_install at all? It won't synchronize your macports or anything like that, but it will automatically download the latest package and all necessary dependencies, i.e.
easy_install nose
for the nose unit testing package, or
easy_install trac
for the trac bug tracker.
There's a bit more information on their EasyInstall page too.
For MacPython installations, I found an effective solution to fixing the problem with setuptools (easy_install) in this blog post:
http://droidism.com/getting-running-with-django-and-macpython-26-on-leopard
One handy tip includes finding out which version of python is active in the terminal:
which python
When you install modules with MacPorts, it does not go into Apple's version of Python. Instead those modules are installed onto the MacPorts version of Python selected.
You can change which version of Python is used by default using a mac port called python_select. instructions here.
Also, there's easy_install. Which will use python to install python modules.
You may already have pip3 pre-installed, so just try it!
Regarding which python version to use, Mac OS usually ships an old version of python. It's a good idea to upgrade to a newer version. You can download a .dmg from http://www.python.org/download/ . If you do that, remember to update the path. You can find the exact commands here http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/
I use easy_install with Apple's Python, and it works like a charm.
Directly install one of the fink packages (Django 1.6 as of 2013-Nov)
fink install django-py27
fink install django-py33
Or create yourself a virtualenv:
fink install virtualenv-py27
virtualenv django-env
source django-env/bin/activate
pip install django
deactivate # when you are done
Or use fink django plus any other pip installed packages in a virtualenv
fink install django-py27
fink install virtualenv-py27
virtualenv django-env --system-site-packages
source django-env/bin/activate
# django already installed
pip install django-analytical # or anything else you might want
deactivate # back to your normally scheduled programming

Categories

Resources