How do I configure Python and organise my interpreters? - python

Background:
I have been learning Python (on MacOS) for about three months now. I began by installing Python 3.7 through Anaconda and predominantly used Jupyter Notebook to write code. As I learnt more, I found out about different text editors and IDEs, and tried out different programs: IDLE, Sublime and PyCharm.
As I progressed I also learned about package management systems, such as pip and conda, and other such wonderful things that helped me achieve the things I wanted.
The Problem:
Problems surfaced after I installed PyCharm and Python 3.9.
My computer now has four or five different versions of Python installed (2.7, 3.7, 3.8, 3.9) - some which I seem to have downloaded multiple times - and now nothing works any more! The modules I have installed using pip don't load properly any more, my gorgeous programs (some of which are over 600 lines long) don't run any more - I can't even run a simple print('hello world') any more.
My files are a mess. I have probably over a 500mb of Python related junk that I don't use. I have tampered with PATHs and bins in the console, and I don't even know how to begin to sorting this out.
What I Want:
An ideal solution for me would be to just have PyCharm and Jupyter Notebook installed and for them to both be able to import the modules I have installed. I would also like to remove older versions of Python and just have both Jupyter Notebook and PyCharm use 3.9 as their interpreter. Additionally, I would like to safely delete all the junk files that I have collected over the last few months, though I understand that you might not be able to help me with that.
In short: I just want everything to work again!
I spent weeks writing a program that was basically finished and now doesn't even make it passed the second line without throwing out an error.
What can I try next? I am open to anything, including backing up my files and resetting my whole mac (though I'd like to reserve this as plan C).

I have solved my own problem but it took quite a bit of investigation, and will outline what I did:
Reset my Mac (this was not necessary but I wanted to do it anyway)
Install home-brew
Install pyenv
Install pipenv
For anyone experiencing the same problems, you should look definitely into pyenv and pipenv as a way of managing your Python versions and modules.
I won't go into too much detail, as plenty of other already have, but will post some links to some good articles that helped me:
https://opensource.com/article/19/5/python-3-default-mac
https://realpython.com/intro-to-pyenv/
https://pipenv-fork.readthedocs.io/en/latest/
Hope this helps.

Related

General question about python interpreters (having multiple interpreters, pyenv, etc)

I'm new to coding and I was wondering if someone could explain what exactly python interpreters/environments do and the how that relates to the python versions and packages that can be used in certain projects.
I was trying to code a twitter bot with python and I use VS code as my IDE. While trying to import tweepy into my python code, I noticed I kept getting an error that it couldn't import. After some googling I realized it was because I had to set the python interpreter of the python file to the one that had tweepy installed to. So I changed it and the error with tweepy was solved. But I noticed that I have a bunch of python interpreters in seemingly different locations (screenshot of the interpreter options I'm given). The interpreter that fixed the issue was the one in the pyenv path.
I had installed pyenv some months back because I wanted my terminal (I'm on macOS) to automatically launch python3 when I typed python into the terminal. However, I don't really know what it does beyond that.
So my questions are:
Why do I have multiple python interpreters? Is there a way to get rid of the ones and just keep the one from pyenv (like clean up the ones in /usr/local/bin/python3, /opt/homebrew/bin/python or /usr/bin/python3 since I'm not using them)? or should I not do that?
What exactly does pyenv do? Is it okay that my python libraries are getting installed to pyenv by default? Or should I change it so that it's getting installed to homebrew or one of the usr/bin paths?
Sorry, this is my first time asking a question here so I might not sound that cohesive.
The interpreter is the program which executes a python source file. This is a program like any other, and you can have various versions of it at once (indeed python is quite good at being self contained, and multiple versions will live alongside one another quite happily).
Macs ship with an outdated python 2, used for something in the system. If you remove or update it, things may break. So you install your own python 3 somewhere. If you want to load the new python when you type python in a shell, that shell's $PATH needs to point to the right python. You generally set this in something like ~/.profile.
Pyenv is a tool for managing multiple python interpreters. It's usually used on a per-project basis, to test code against multiple pythons. Further to complicate things, it's often used alongside a virtual environment tool like pipenv. Using it to avoid manually fiddling with $PATH is fine, but a slightly orthogonal use case. All pyenv does is to put little scripts with the same name as python executables somewhere in your $PATH where they supercede anything else. These scripts then figure out which python should get called in this case. So pyenv isn't installing anything, it's just working out which python is going to do it.
The solution to all your problems turns out to be quite simple:
python -m pip install abc
Get into the habit of installing stuff with the python you want to use already set up in the shell, and calling pip like that. That way whatever you use it will always be installed in the right env.

Installing linters on Atom using a MacBook

I am a beginner coder and have installed anaconda and atom on my MacBook. So far everything was working well - until I tried to install linters into Atom. I tried linter-flake8 first with the terminal using pip install flake8, next in atom I installed linter-flake8 and it’s dependencies. Still nothing was happening and no error highlights appeared even when I made deliberate mistakes in a python file. The same problem is happening with linter-pylint.
Can you please advise in the simplest way with basic steps I can take to solve this? Most of the solutions provided online are quite complex and I can’t understand them. Also most solutions center around windows operating systems.
Many thanks
First install this package https://atom.io/packages/linter
and you can use this second package for python language https://atom.io/packages/linter-python.
Otherwise you find all linter-package here.

How to install pip for a total newbie

I have read a bunch of posts here and on Google, but my question is far more basic than the answers: If Python(2.7) came pre-installed on my MacBook Pro (High Sierra), can I just do sudo easy_install pip (as suggested) from the command line--withOUT causing issues? I have a vague understanding of global/local installations, and my understanding is certain Python installations aren't compatible with local/global kernel installations. I hope I am getting the terminology right, but I saw several warnings about installing pip for "a homebrew based python installation", but I am not sure whether Python on my laptop is installed via homebrew (nor how to find out).
My question came about because I wanted to install the Hydrogen package to use in Atom, the text editor (to help me learn Python). I finally succeeded in installing Hydrogen, but got stumped by the missing kernels (not sure which ones I need, so I am willing to install them all). But I can't seem to install the kernels without pip. So here I am.
My apologies for asking such a basic question--and thanks!
The rule of thumb is: If your operating system has a package manager, use it.
Unfortunately, MacOS is the only UNIX-like operating system that does not come with a decent package managment system.
(There is the app store, but that is useless for a lot open source software for several different reasons. It's also a walled garden.)
You have several choices (in descreasing suitability):
Use one of the package managers available for MacOS. Which one is the best choice for you depends on all the packages you need being available.
Use a Python distribution. I've used Anaconda on ms-windows, and that has saved me a lot of hassle. A good choice if you are only looking for Python and related libraries.
Build everything yourself. This can be very time-consuming and is a duplication of effort. You will learn a lot though.
I would second Piinthesky's comment that you install Python 3.6. Python 2.7 is now a legacy version.
Well although I am no Mac expert I've given it a shot anyway:
Yes you could but do you really want to risk it (or even do it)?
Mac-OS must rely on Python to fulfill something in the OS otherwise it would not come inbuilt. This means two things:
The Python installation will be minimal. By that I mean it will have things missing (any large library for a start). They will do this mainly to cut down on the OS size. Therefore you will not have the full Python library and in the long term you may end up missing out.
Second if anything went wrong (IE you broke your installation or even modified it -yep I've done this in Linux and have ended up factory resetting) then you may cause something to stop working and may need to factory reset or perform some other drastic action on your OS. A separate installation would prevent your from risking this. This is very useful because there comes a time when you may decide to update certain modules with pip and find it can't or it updates something that you shouldn't be messing with.
Yes it's possible you may run into compatibility problems but I think it's most widely accepted that you do not use the inbuilt one as it needs to remain unchanged if the OS is to use it correctly. Messing with it increases the chances of it breaking.
Conclusion: So even though installing modules with pip (and getting pip) can be done with the inbuilt Python it comes down whether you want to risk harming your OS. I strongly suggest you get a separate installation and leave the inbuilt one as it is. Second as you mentioned you will find that the inbuilt versions are never up-to-date or are built were they are not really compatible with standard libraries (expect things like the missing runtime libraries all the time) , just another reason to stay clear of them.
This is how I solved this problem-for those newbies who just want Hydrogen to work:
Installed Python 3 (instead of messing around with Python 2.7 and pip).
Followed instructions here (https://packaging.python.org/tutorials/installing-packages/#ensure-you-can-run-pip-from-the-command-line) for 'get-pip.py'.
In Atom, cmd+shift+p to bring up the packages menu, clicked on 'Hydrogen Run', which gave me the errors again.
Copied the code from the warnings and installed the kernels needed (via the command line).
Hydrogen is now working.
Thanks for all the tips!

How to uninstall and/or manage multiple versions of python in OS X 10.10.3

I have installed the Python IDE Spyder. For me it's a great development environment.
Some how in this process I have managed to install three versions of Python on my system.These can be located as following:
Version 2.7.6 from the OS X Terminal;
Version 2.7.8 from the Spyder Console; and
Version 2.7.9rc1 from an IDL window.
The problem I have is (I think) that the multiple versions are preventing Spyder from working correctly.
So how do I confirm that 2.7.6 is the latest version supported by Apple and is there a simple way ('silver bullet') to remove other versions from my system.
I hope this is the correct forum for this question. If not I would appreciate suggestions where I could go for help.
I want to keep my life simple and to develop python software in the Spyder IDE. I am not an OS X guru and I really don't want to get into a heavy duty command line action. To that end I just want to delete/uninstall the 'unofficial versions' of Python. Surely there must be an easy way to do this - perhaps 'pip uninstall Python-2.7.9rc1' or some such. The problem is that I am hesitant to try this due to the fear that it will crash my system.
Help on this would be greatly appreciated.
(Spyder dev here) There is no simple way to do what you ask for, at least for the Python version that comes with Spyder.
I imagine you downloaded and installed our DMG package. That package comes with its own Python version as part of the application (along with several important scientific packages), so it can't be removed because that would imply to remove Spyder itself :-)
I don't know how you installed IDL(E?), so I can't advise you on how to remove it.

Installing Python packages on Windows

I'm at a bit of despair.
I've spent the last two days trying to set-up a proper Python (2.7x) Windows enviornment.
I tried a bunch of different "bundles", and it boils down to this:
It's either I can't install the packages I want;
or I can't use the IDE I want.
I tried a simple install of "python" with no additions.
Very early, I come to a stop while using pip/easy_install, or exe files to install additional packages.
I then tried "Portable Python" which weathered much better.
It has a nice (though limited) bunch of packages.
I can use it with any IDE I want (usually PyDev/Eclipse).
Here too, It's practically impossible (for me, following what I read) to install most packages.
I then went with Enthought Canopy (formerly EPD).
This has a superb tool for package managing. I did not find any problem, with package handling.
However, it comes with a very limited (almost insulting) IDE.
All my actions to point eclipse interpreter to the Canopy directories, went awry.
Lastly, I tried WinPython.
This is somewhere in-between "Portable" and "Canopy".
It has more packages pre-bundled, than "portable" has, but less than canopy.
It also has a package management GUI, but unlike canopy's, this one fails.
Please, tell me how you set-up a Win/Python extendable enviornment.
And please, do not flag this as a duplicate - I have read dozens of related questions here on SO, and elsewhere. Non actually helped me.
I would not be wasting your times, unless I tried all paths before (obviously using SO and Google to solve my problems before going through another lengthy install).
[EDIT (SOLVED) ]:
I uninstalled everything, and then installed "plain" python.
I also manually removed all references to orphaned "pythons" of past from the PATH and PYTHONHOME env vars.
That seems to work.

Categories