Python: installing packages on Mac - python

I tried to install the pyqt5 package für python 3.9 on Mac.
Since it didn't work via pip3 I installed it via Homebrew
% brew install pyqt5
and it seemed to work since the terminal says, if I try
pip3 install pyqt5
the requirements are already satisfied.
Yet, if I start PyCharm to to write and test my program it can't find the libraries etc.
What do I have to do?

I just tried to brew install pyqt5 on my Mac. brew then installed a new Python 3.9 interpreter (this is viewed as a dependency of pyqt5).
I suspect the same has happened on your system: brew has installed a whole new Python interpreter, and now when you type pip3 at the command line, you're finding the version of pip installed by brew, which is telling you that pyqt5 has been installed.
However, each Python installation on your system as its own set of packages. Your new Python interpreter has pyqt5 installed, but your original Python interpreter still doesn't.
I suspect that PyCharm is configured to use your default (original) Python interpreter, which doesn't have pyqt5 installed.
Try executing the following at your command prompt (terminal): which pip3. If you're shown a path /usr/local/Cellar/... then this confirms that when you type pip3 at the command line you're actually referring to the version of pip corresponding to a Python interpreter installed by brew.
OK, so what to do going forward?
Two options:
Work with this new Python installation. Then, you'll need to install all of the packages in your previous version of Python again (e.g. just because you had Numpy installed on your original Python installation doesn't mean you'll have it installed by default for your new Python interpreter). Further, you'll have to configure Pycharm to use this Python installation. I don't know the exact steps for this, but go to the Preferences tab and look for something along the lines of 'Python Interpreter' underneath 'Project Settings'.
Remove the new Python installation, figure out what's up with pip, install your desired package.
I'd go with 2). It can get messy to have multiple different versions of Python on your system, unless they're managed by an environment manager such as conda.
To that end, what went wrong when you first tried to install pyqt5?

Related

How to reinstall all user packages after updating Python version in Windows?

I have a Windows 7 machine running Python 3.8.5 with a very large number of physics/electronics/data analysis/simulation packages. As it turned out, I must have - for some inexplicable reason - installed the 32-bit version of Python instead of the 64-bit one despite having a 64-bit system. And I didn't notice until very recently when I was trying to install some packages that require 64-bit Python. Hence I've now downloaded and installed the latest Python version that is supported by Windows 7, which seems to be 3.8.10.
Question: What is the easiest and also fail-safe way to reinstall all the user packages - that I currently have under 3.8.5 - to 3.8.10?
For some reason, I couldn't find any "canonical" solution for this online. As it seems, Python does not come with any built-in support for updating or system migration and I'm honestly wondering why...
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how. Reason: Doing help('modules') inside the interpreter will list all packages and I don't see a way to "selectively apply" pip to a specific Python version, e.g. something like python-3.8.5 -m pip list --local is not supported.
After getting a list of the user packages, I was thinking to pack it into a batch command pip install package_1 package_2 <...> package_N, thus reinstalling everything to Python 3.8.10. And afterwards uninstalling Python 3.8.5 and removing all environment variables from system PATH.
Is this the proper way to do this?
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how.
Create a list of installed packages with pip freeze > pkglist.txt or pip list --format=freeze. If you already have one, that's great.
Then uninstall 32-bit Python 3.8.5 and clean your path for all Python related variables. Now, install 64-bit Python 3.8.10.
After reinstalling, you can install back all the packages with pip install -r pkglist.txt and it will restore the exact versions of the packages.
If you insist on having both 32-bit and 64-bit versions installed and also have the Python Launcher installed, you could invoke 32 and 64 bit versions separately with py -3.8-64 -m pip and py -3.8-32 -m pip.
I don't see a way to "selectively apply" pip to a specific Python version.
This is possible with the Python Launcher on Windows. But only between major/minor versions and not the patch versions according to its help message.
I would also recommend creating a virtual environment this time before installing the packages and leaving the root environment alone. You can create one named venv with just python -m venv venv, activate it with ./venv/Scripts/activate and proceed with the installation of packages.
Nope, doesn't work. After installing the packages with the newer Python version in PATH, e.g. Jupyter won't start.
If the Jupyter error persists, you could try pinning packages to their most recent patch/minor versions to update them and yet not break your code.
As a last resort, you could try installing Python 3.10 alongside your current Python installation (without uninstall or editing the PATH) and then installing the absolute latest versions of the packages in a 3.10 virtual environment to see if it works for you. You would invoke the two versions with Py Launcher, e.g. py -3.10 and py -3.8.
If I understood correctly, you have multiple packages like NumPy, pandas etc. installed on your machine, and you want to reinstall them "automatically" on a fresh installation of python.
The method (I use) to perform such an operation is by creating a file named setup.py which includes a list of all the packages.
Bellow, I am attaching an example of such a file I use in one of my projects:
from setuptools import setup, find_packages
setup(
name='surface_quality_tools',
version='0.1',
install_requires=["matplotlib", "psutil", "numpy", "scipy", "pandas", "trimesh", "pyglet", "networkx", "protobuf",
"numpy-stl", "sklearn", "opencv-python", "seaborn", "scikit-image", "flask", "tqdm", "pytest"],
package_data={'': ['*.json']},
packages=find_packages(include=[])
)
to run the installation you should open a command prompt from inside the project directory and run:
pip install -e .
You can find a nice example in this blog page
One common way of handling packages in Python is via virtual environments. You can use Anaconda (conda), venv or any of several other solutions. For example, see this post:
https://towardsdatascience.com/virtual-environments-104c62d48c54#:~:text=A%20virtual%20environment%20is%20a,a%20system%2Dwide%20Python).
The way this works in by keeping the Python interpreter separate from the virtual environment that contains all the necessary packages.
Probably the main reason Python doesn't feature migration tools (at least as part of standard library) is because pip - the main package tool - doesn't handle conflict resolution all too well. When you update a version of Python it might so happen (especially with niche packages) that some of them won't work any more and pip often won't be able to solve the dependencies. This is why it's a good idea to keep a separate venv for different Python versions and different projects.
The other tool you could use for easy migration is Docker which is a semi-virtual machine working on top of your host OS and containing usually some linux distribution, Python along with the necessary packages necessary for running and development.
It takes a bit of time to set up a container image initially but afterwards setting everythin on a new machine or in the cloud becomes a breeze.
Listing currently installed packages is done via pip freeze command, the output of which you can then pipe into a file to keep a record of project requirements, for example pip freeze > requirements.txt.

Unresolved import

I get this error for every single package that I have downloaded from pip whenever I type import [package] or any variation there off. This also means that the packages do not work. This is strange, since it used to work.
Edit: Apparently it works just fine on Jupyter Notebooks, but not in a normal python file.
it seems that you are installing the module in a wrong way, specifically, to improper directory (or python version).
you can alway check if module/package is installed or not by this:
pip list
if you can see the module in list, then it is installed, if not, then you need to install it properly.
sometimes you may have different versions of python installed, you install the package to version X and try to run it on version Z. all you need to do is to clarify your python version, check if the pip is referring to the python version by doing this:
pip -V
it shows the pip version along with the python version it is referring to.
The BEST WAY
always try to create a virtual environment and install packages there.
creating a virtual environment
python -m venv <VENV_NAME>
then activate it:
source <VENV_NAME>/bin/activate # MacOS & Linux
<VENV_NAME>/Scripts/activate # Windows
then, install your packages. there shouldn't be any problems after that.

Python 3.9 pip install

I recently downloaded and installed Python 3.9 because I wanted to run a website scraper to more easily organize recipes found online. However, when I try to run pip it says it doesn't recognize it (and I have tried editing the path but every video or site I find has different information).
Even a basic check of my Python version comes back with no results:
I have uninstalled and reinstalled Python 3.9 but to no avail. There is also no scripts file within my python file and my computer doesn't even seem to recognise that Python is installed.
In python 3.9 you can add below path(scripts path) to your environment variable
C:\Users\ASUS\AppData\Local\Programs\Python\Python39\Scripts
Once the path is added, open a fresh CMD window and type pip --version.
You can see your pip version pip 20.2.4
I found that for Python 3.9 if you enter the command as py -m pip install, the installation initiates as expected.
Annotation 2021-06-17 121518_install pywinauto Visual Studio Code terminal
I finally can use pip install.
Here is how I do it:
Run python 3.6.9-adm64.exe
Choose Modify
Tick all boxes and click Next
Tick [Create shortcuts...appplication](not important) and Add Python to environment variables and click Install
Now run CMD and type:
py -m pip install (name) //pygame for example
Now I installed pygame easily.
I have fixed this issue by running apt-get install python3-pip
on my Debian Linux.
every video or site I find has different information
This may be due to the fact that those sites provide information for different Python versions.
As Alfie Hanks already pointed out in the comments: The right way to do it is to check the box Add Python 3.x to PATH when installing Python 3 for the first time. When re-running the installer / re-installing choose "Modify" and check the box Add Python to environment variables. That takes care of setting the correct environment variable so that the Python and pip binary can be found by Windows.
If you have trouble finding those options, take a look at circlepi314's answer to a similar Python question: https://stackoverflow.com/a/54029728/6710751 This one has screenshots of the installer where those options are marked.
Install python again and when the prompt window opens, click on the modify button. Then check if pip is checked. If not then check it and then proceed with the install.enter image description here
If it still doesn't work, simply uninstall python. And then when you reinstall it make sure that the ADD to path checkbox is checked.
I had the similar problem, I managed to solve it with the following:
I installed python 3.9 by Brew on my Mac, so the pip3 was broken
I resolved by uninstalling python3.9 and installing 3.8

Canopy--python and terminal Python, relations and differences

I've been using Canopy and IPython notebook especially, most of the time. I installed Python3.4 from Python.org at the very beginning. Since then every time I run my IPython notebook and if there is any packages that I am missing, I download them by using the package manager in Canopy. However, recently I started that tango with Django tutorial project and used a lot of command lines. I installed Python2.7 later, and the related packages by using pip ever since. My question is: what's the relation of Canopy package manager and pip? Because I am not afraid I might have downloaded the same packages and created unnecessary duplicates more than once by using them separately and simultaneously...Thanks
Type:
which pip
It will tell you if pip is pointing to your canopy python directory, or to your system python directory. I believe that pip will use whatever your system's default python is. You can figure this out by typing:
which python
Generally, when I put canopy on a computer, I make canopy the default python (it will prompt you the first time it starts up, and you can set it in the options later). After I do this, pip will install packages to the canopy python directory. This layout usually works best for us, because you can still use:
python setup.py install
To install packages to canopy.

noob, but I installed python 2.7.5 on my mac, how to I "target" that one rather than the built in 2.7.2?

I went thought and installed pip and then added a bunch of libraries that I like to use and then, only after installing everything, did I realize that everything went into the 2.7.2 sit-packages directory, so the Python2.7.5 version doesn't see anything.
Now, If I type python --version in the terminal, the correct version is started. However, pip is still "tied" to the default version of Python.
How do I go about telling OSX to look at the new version of Python for everything?
Honestly, one way around this is to make sure that virtualenv works with the right version, and just use pip inside the virtualenv.
A common pattern of Python installation on the mac is to use Home Brew which is a package manager for the mac. You can then install python using:
sudo brew update
brew install python
Provided you have the XCode command line tools already installed.
After I think that the Home Brew Python will be the first in the path. If this is not the case, it might be simpler to use a virtualenv by installing the package with the pip provided by the Mac Brew install (/usr/local/Cellar/python/2.7.5/bin/pip).
After this is done you need to create a virtualenv
virtualenv ~/path/to/the/env
and to activate it
source ~/path/to/the/env/bin/activate
This will be a brand new python and your path will be configured correctly (the python and the pip will be the right ones). You can always delete it, deactivate it or source it as needed.

Categories