How to use and install all the libraries of Python3? - python

Using Ubuntu 16.04.
I was doing development in Python2.7, although recently moved to Python3.5, both of which come by default. The problem is, I find all the python libraries have to be reinstalled or downloaded for the next Python3. Also, the behaviour of Python seems weird because to execute the same script in python3, I have to enter in terminal
python3 script.py
which is different from other applications where I do not have to give the version number. Anyway, the questions I am trying to find answers are
To what extent are the libraries, packages (such as pip etc.) shared between 2.7 and 3.5? Or do I need double installations (and double the space) for everything now? A bit space limited in my old laptop.
Most of the installation instructions and commands I find online do not specify whether they are for 2 or 3. Given that I have two versions, how do I control/make sure they go to 3.5?
Is it advisable and possible to completely remove everything related to 2.7? Can I keep working with 3.5?

The first thing you need to know is that all official libraries and python tools for python3 got the "3" character to separate them from the previous versions. So, you need to use pip3, and not pip, python3, not python, and the packages are called python-pygame, not python-pygame.
So, to answer to you in order:
Yes, the you have to double the space needed if you decide to use both python2 and python3
Usually, if in the tool name there is 3, it is for python3, and if not it's for python2
Python2 and python3 are completly indipendent (different path, indipendent versions, etc.) so having python 2.7 installed doesn't affect python3 BUT since python 3 is the next version of python 2, it makes it obsolete (in my opinion) so if you don't have enough space for both, keeping python2 is absolutely not needed nor useful

If you want to control your python execution then you have various method or techniques:
For downloading packages according to version
You can use pip{version-name} to download the libraries. Like if you want to download library of python 2.7 then write
pip2.7 install package-name
for python 3.5 then use
pip3.5 install package-name
For execution of program:
If you want to execute the program accoridng to you choice of version then just use
python{version-name} script.py
eg:
python2 script.py
python3 script.py
or you just write down the path of your python version on the top of script. Please refer this for more details:
Why do people write #!/usr/bin/env python on the first line of a Python script?

Related

Install and run Mac Python Open CV

I have python installed, python 3 installed, installed opencv using homebrew, also installed python through the GUI, followed various instructions pages and comments written several years ago.
I'd like to use OpenCV to detect brightest and darkest pixels and draw line between them to carve the image up into cells based on the bright and dark points in each cell. I assume this a reasonable application for this software.
When I go to python, any installation, and enter import cv I get a 'no module' message.
First, you need to decide whether you want to run Python2 or Python 3. I would advocate Python 3, firstly because this is clearly a new project so you may as well use the latest and greatest Python, and secondly since Python 2 is end-of-lifed in 9 days' time.
Then you need to decide if you want to use the Apple-supplied Python, in /usr/bin or the homebrew supplied Python. As you have installed homebrew Python, I would advocate using the homebrew one because you can delete the whole thing and re-install it if you mess it up while all the Apple-supplied patches, OS upgrades and their other uses of their Python will remain unaffected. Happy days!
So, you want to use homebrew Python 3. Now check what homebrew tells you, by running:
brew info python
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been
installed into /usr/local/opt/python/libexec/bin
That means if you want to run python and pip (without a version number, as opposed to python3 and pip3) and expect that to start Python 3 and its corresponding pip, you need to put /usr/local/opt/python/libexec/bin at the start of your PATH in your bash profile (probably $HOME/.bash_profile):
export PATH=/usr/local/opt/python/libexec/bin:$PATH
Then start a new Terminal and check which Python runs when you type python:
type python
And it must report as follows if you are on the right track:
python is /usr/local/opt/python/libexec/bin/python
Then, you can check which Python version that is with:
python -V
and it should be:
Python 3.7.5
Now you can install OpenCV with:
pip install opencv-python
Then load it in the interpreter with:
import cv2
If, for some reason, you want to run the old v2.7 Python, or risk messing up the installation of your macOS by installing clashing modules, just disregard my answer.

What to do after upgrading from Python 2 to Python 3?

Recently I've installed Python 3 after using 2 for quite a while, so I have many libraries for Python that I've installed using pip. I've already started using the symlink python3, but what else do I need to do to change over?
For example, while I have made the alias python=python3, I don't know about the modules I've installed with pip. I was using a version of pip for Python 2, but does that mean modules were written for Python 2 and not 3? Do I now need to start using pip3 (or make a corresponding alias pip=pip3 or pip=pip3.5)? If modules were installed using the command pip, does that mean I have to reinstall them for Python 3 using pip3? If I do need to reinstall them for 3, should I start removing them from my Python 2 env as cleanup?
And another question that seems like it may entail a lot of work: How should I change all my virtual environments for Python projects? They were all Python 2 envs, and now I'm not sure whether it's necessary, useful, or possible to reinstall or upgrade the modules within them to the corresponding ones for Python 3.
Sorry if this seems like a duplicate question - I've searched and only found resources regarding "how to upgrade" rather than "what to do after upgrading". Thanks for your help!
It is strongly recommended not to symlink python to Python 3, at least on Debian-based Linux distros. Debian utility scripts use both Python 3 and 2.7 to work (which means you already had a copy of Python 3 somewhere...), and as a consequence Debian-based distributions ship both versions. Symlinking can cause unexpected breakages. This may or may not be true on other systems, however, but with such ambiguity it is probably useful not to try.
Instead, live with symlinking python3 to Python 3. Leave python alone.
pip is usually symlinked to the corresponding pip program for 2.7. Use pip3 to specify you want to use the pip program for Python 3. If you use pip, you'll find modules for Python 3 being installed to Python 2.7 site directories and nothing will work as you expect it to. There should be no reason why you can't symlink pip to pip3, but I'd still not recommend it - it's better to tread cautiously in this regard.
You don't need to change your current virtual environments. Just create new ones with virtualenv, pointing to the correct Python program to use. This is the whole point of virtual environments: to sandbox different Python versions, so that neither of them adversely interact with each other.
If you have projects you really want to ship to Python 3, I'd recommend using a version control system to back up your current files, and then reproducing them in a new virtual environment configured for Python 3. This is quick, simple and painless.

Use system python in homebrew

Is it possible to use system python in homebrew?
I have python 2.7.5 installed on my mac, but when I try to install any homebrew package with python in dependencies, it starts loading python 2.7.9.
It is important for me to use system python because of lots of installed python packages.
What version of python homebrew uses depends on two things:
Where the python alias points to.
What the python path is in the formula.
In most cases, homebrew forumlae simply use the python command, and does not specify a python path. If it does, then it is usually /usr/bin/python.
So, you can fix the problem in two ways:
Change the homebrew formulate to point to the right python interpreter of your choice.
Change where the python command points to.
The first option is problematic, since it means that you need to go into your Cellar folder, and alter things. This is not nice, and usually not required.
A better alternative is to point to the right python command. This is where pyenv comes in handy. pyenv is a command line tool that helps you manage different versions of python. Follow the instructions here:
https://github.com/yyuu/pyenv-installer
to install it.
Once that is done, change your python command to whatever version of python you'd like it to point to.

portable python 3.x and pygame

I am looking for version (custom/beta?) of portable python that is python 3.x and includes pygame. I know pygame hasn't been fully converted to 3.x yet, but for what I need it for it works perfectly.
Install Pygame, then take your entire Python folder and put it where you want it to go.
If you mean you want to be able to use python (filename).py on the terminal, then you will have to change the PATH variable in the terminal, or add the shebang line #!usr\bin\python onto your programs.
Maybe I have not fully understood what you're after. Still, let me try to answer:
The release of Python 3.3 now includes VirtualEnv in the standard library, now called venv.
So in a command line you can type
/path/to/python3.3/bin/pyvenv /path/to/new/virtual/environment
To create a folder with links to your python 3.3 installation. That new folder makes it easy to install any PyPi packages into, for example:
cd /path/to/new/virtual/environment
./bin/pip install adventure
to install Adventure.
You then probably want to follow PyGames documentation to install the Py3 branch
Then, like user beary605 already answered you can bundle up your whole virtual environment of python by tar'ing, rsync'ing or whatever means of distribution you envision.
There's also an older question covering Python 3 and Pygame compatibility on Mac OS.
To my knowledge there is nothing like what you are looking for available, however with a little DIY you might be able to alter a version of Portable Python (it's not updated anymore, but there's a version for Python 3). By saying this I am presuming you want to operate this from a flash drive. Depending on what Windows you are running the steps might be a bit different, but I'm guessing they would be similar to the steps in this thread: pygame not working with portable python

Proper Unix (.profile, .bash_profile) changes for Python usage

I new to Python and to programming in general. I'm a novice, and do not work in programming, just trying to teach myself how to program as a hobby. Prior to Python, I worked with Ruby for a bit and I learned that one of the biggest challenges was actually properly setting up my computer.
Background: I'm on a Macbook with OSX 10.7.
With Ruby, you have to (or rather, you should), edit your ./profile and add PATH info. When you install and use RVM, there are additional items you need to add to your bash_profile.
Do you have to make similar changes with Python? What are the best practices as I'm installing/getting started to ensure I can install modules and packages correctly?
python works out of the box on OS X (as does ruby, for that matter). The only changes I would recommend for a beginner are:
1) Python likes to be reassured that the terminal can handle UTF-8 before it will print Unicode strings. Add export LANG=en_US.UTF-8 to .profile. (It may be that the .UTF-8 part is already present by default on Lion - I haven't checked since Snow Leopard.) Of course, this is something that will help you in debugging, but you shouldn't rely on it being set this way on other machines.
2) Install pip by doing easy_install pip (add sudo if necessary). After that, install Python packages using pip install; this way, you can easily remove them using pip uninstall.
Take a loot at Python on the Macintosh page first. Like it says, Python comes pre-installed on Mac OS X. It means that you don't have to do anything special in order to use it.
To get started, you can run a Terminal.app, type python and that will get you Python interactive shell up and running.
However, Python on OS X might be of a slightly older version. For example, OS X 10.7.3 comes with Python 2.7.1, whereas latest release version of the Python is 3.2.3. If you want to use other versions, then you will have to install them. Then it all depends on what, where and how you install. If you want to have multiple versions alongside, you may need to set some environment variables like PATH to have binaries you installed found by the bash etc. You can do it through bash ~/.profile if needed.
But until you get to that point - don't worry about it use a version shipped with OS X. Once you want a newer one - download and install it. Then, if it doesn't work out of the box or you have any other problems or concerns, feel free to ask a more specific question.

Categories