How can I tell if Python setuptools is installed? - python

I'm writing a quick shell script to make it easier for some of our developers to run Fabric. (I'm also new to Python.) Part of installing Fabric is installing pip, and part of installing pip is installing setuptools.
Is there any easy way to detect if setuptools is already installed? I'd like to make it possible to run the script multiple times, and it skip anything it's already done. As it stands now, if you run ez_setup.py twice in a row, you'll get a failure the second time.
One idea I had was to look for the easy_install scripts under the /Scripts folder. I can guess at the Python root using sys.executable, and then swap off the executable name itself. But I'm looking for something a little more elegant (and perhaps cross-OS friendly). Any suggestions?

Try with this command.
$ pip list
It returns the versions of both pip and setuptools. Otherwise try with
$ pip install pil
If this also doesn't work, then try with
$ which easy_install

This isn't great but it'll work.
A simple python script can do the check
import sys
try:
import setuptools
except ImportError:
sys.exit(1)
else:
sys.exit(0)
OR
try:
import setuptools
except ImportError:
print("Not installed.")
else:
print("Installed.")
Then just check it's exit code in the calling script

Just run the following code into IDLE:
import easy_install
If it just goes to the next line, I think it's installed.
If it says:
Error: invalid syntax
Then it probably isn't installed.
I know this because I tested pip with it.
Also just check import pip to see if pip is pre-installed. :)

you can check for easy_install and setuptools by running the following command line commands:
which easy_install
#finds the path to easy_install if it exists
less path/to/easy_install
#where path/to/easy_install is the output from the above command
#this outputs your easy_install script which will mention the version of setuptools
If the easy_install/setuptools bundle is installed, your output from the second command above will probably read something like this:
#EASY-INSTALL-ENTRY-SCRIPT: 'setuptools==0.6c11','console_scripts','easy_install'

It comes preinstalled with new versions of Python.
pip3 list
was enough to identify it was installed for me

This will display the version of your setuptools if it is installed already
$python -c "import sys; import setuptools; print(setuptools.version.__version__)"

Depends with the python version installed. you can try "pip list" or "pip3 list" and check for the setuptools and version installed.

Related

ModuleNotFoundError: No module named 'schedule'

I have python program that imports schedule (import schedule) at the beginning. The code executes without a problem with python3 command, but starting it from other python file with call("sudo python3 ProgramWithSchedule.py", shell=True) returns error ModuleNotFoundError: No module named 'schedule'. And I can't figure out why...
I have library schedule installed with pip, pip3 AND apt-get (tried all three just to be sure :)
Thanks!
Because you are using a different interpreter/virtual environment for each project, which is generally considered the best practice.
You can apply the command below to create a file with all your installed modules, so you can use them whenever you want, by a single command to install all.
To keep/save all modules in a file:
pip freeze > requirements.txt
To install all of them with a single command in a new interpreter/virtual environment:
pip install requirements.txt
In case you tried installing a package and get an output:>>Requirement already satisfied.
You will find a path in your output where it says Requirement already satisfied, copy the path. Now go back to your working environment.
import sys
sys.path.append("/the/path/you/copied")
import schedule
You can try to force the usage of the same python interpreter with :
call(f"sudo {os.getenv('PYTHON3')} ProgramWithSchedule.py", shell=True)
and call your-script.py with :
PYTHON3=$(type python3) your-script.py ...

Difference between "pip install" and "python -m pip install" [duplicate]

I have a local version of Python 3.4.1 and I can run python -m pip install, but I'm unable to find the pip binary to run pip install. What's the difference between these two?
They do exactly the same thing. In fact, the docs for distributing Python modules were just updated to suggest using python -m pip instead of the pip executable, because it's easier to tell which version of python is going to be used to actually run pip that way.
Here's some more concrete "proof", beyond just trusting my word and the bug report I linked :)
If you take a look at the pip executable script, it's just doing this:
from pkg_resources import load_entry_point
<snip>
load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
It's calling load_entry_point, which returns a function, and then executing that function. The entry point it's using is called 'console_scripts'. If you look at the entry_points.txt file for pip (/usr/lib/python2.7/dist-packages/pip-1.5.4.egg-info/entry_points.txt on my Ubuntu machine), you'll see this:
[console_scripts]
pip = pip:main
pip2.7 = pip:main
pip2 = pip:main
So the entry point returned is the main function in the pip module.
When you run python -m pip, you're executing the __main__.py script inside the pip package. That looks like this:
import sys
from .runner import run
if __name__ == '__main__':
exit = run()
if exit:
sys.exit(exit)
And the runner.run function looks like this:
def run():
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
## FIXME: this is kind of crude; if we could create a fake pip
## module, then exec into it and update pip.__path__ properly, we
## wouldn't have to update sys.path:
sys.path.insert(0, base)
import pip
return pip.main()
As you can see, it's just calling the pip.main function, too. So both commands end up calling the same main function in pip/__init__.py.
2021
This only happens if you create the venv with PyCharm. Please check if Scripts/pip-script.py located in your virtual environment
pip install and python -m pip install -- is not really the same. Or welcome back into the HELL of VERSIONING & DEPENDENCIES :-(
I was used to type pip(.exe) install <name> if I want install a package. But I run into trouble, if I try to install package Pillow. It breaks every time with an error message.
Today I retry python -m pip install copy&pasted from the manual and it works. Before I ignored it and type pip.... Because I thought it is the same.
I start to dive a little bit deeper into pip and I find this question/answer. After a while I found that pip.exe calls the script <virtual-environment/Scripts>pip-script.py.
I fighting with the installation of package Pillow.
#! .\venv\Scripts\python.exe
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==19.0.3','console_scripts','pip3'
__requires__ = 'pip==19.0.3'
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('pip==19.0.3', 'console_scripts', 'pip3')()
)
I was a little bit surprised that pip.exe still use the old version 19.0.3 of the package and not the new installed version 21.0.1.
I changed the two version strings by hand to 21.0.1. And now pip.exe was able to install Pillow proper.
From now I understand why pip still complains that I use an old version of pip.
I think the old v19 pip has problem to detect the supported platform and therefore sources instead of binaries are installed.

ModuleNotFoundException youtube-dl module is not recognized

i have installed the youtube-dl module via pip with pip install youtube-dl and it worked. I can use it in CMD but for some weird Reason Python says that the Module doesnt exist:
Extension 'cogs.music' raised an error: ModuleNotFoundError: No module named 'youtube_dl'
this problem usually happens when there is more than one python version on the computer, download the modules according to the version you want to use
if this is didn't work for us do like bottom steps
go your python libs path and look up you must got this ........\lib\site-packages\youtube-dl
if you havn't got, go pypi site and download packets
after that, take out this file to ......\lib\site-packages
you should see like this :
or you can do like this:go your python path and open cmd and start writing:
......\python.exe -m pip install youtube_dl
Try the following command.
pip3 install --upgrade youtube-dl

No module named 'pynput'

I am completely new to Python and and still in my babysteps at coding and can't get this thing to work.
I am trying to build an auto-clicker as a learning experience, so I use pynput:
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
But I get the error:
from pynput.mouse import Button, Controller
ModuleNotFoundError: No module named 'pynput'
As troubleshooting I again typed in the cmd "pip install pynput" and got:
Requirement already satisfied: pynput in c:\program files (x86)\python\python37-32\lib\site-packages (1.4)
Requirement already satisfied: six in c:\program files (x86)\python\python37-32\lib\site-packages (from pynput) (1.12.0)
Just to be sure, I also tried "pip3 install pynput" with the same result. When I am in the IDLE and type in "import pynput", I get no errors. I only have one python version installed.
Do you have any ideas what I am still doing wrong?
If you need any more information, just let me know.
Thank you in advance.
JM
You should check the Interpreter the PyCharm uses for your project here:
File -> Settings -> Project: %Project_name% -> Project Interpreter.
It should be same as where you installed pynput.
There might be one of these possibilities to this problem:
The package was not correctly installed. Uninstall it and install it again and see if issue persists.
There could be permission issue on the path where the package is installed. Does it have full rw permissions so python can access it? If you are using linux, use "sudo pip install"
If you have installed the package inside a virtualenv and running the program outside the virtualenv, the package will not be available.
I had a same problem with pynput module.
I fixed my problem in the below.
I checked my python file name and it was a "pynput.py"
This may call my file as pynput module.
So, I changed my file name "pynput.py" --> "pynput1.py"
And, it works well!!
I really hope it can resolve your problem
You probably have multiple python installations and the one used by pycharm is not the one linked with the pip binary.
To solve this issue is it enough to install the library using pip as a module.
Step 1: understand what python interpreter you are actually using
import sys
print(sys.executable)
the output is your path_interpreter (something like /Users/xyz/bin/python)
Sept 2: run pip with that interpreter
from terminal: path_interpreter -m pip install pynput
That's it.
UPDATE: if you get failed to acquire X connection: No module named 'tkinter', try sudo apt-get install python3-tk
If your using PyCharm, try going to the terminal shell (that's built in PyCharm), and type pip install pynput.
If you are using any different IDE, go to your device terminal and type the same thing.

How to install python-dateutil on Windows?

I'm trying to convert some date/times to UTC, which I thought would be dead simple in Python - batteries included, right? Well, it would be simple except that Python (2.6) doesn't include any tzinfo classes. No problem, a quick search turns up python-dateutil which should do exactly what I need.
The problem is that I need to install it on Windows. I was able to upack the .tar.gz2 distribution using 7-zip, but now I'm left with a collection of files and no guidance on how to proceed. When I try to run setup.py I get the error "No module named setuptools".
If dateutil is missing install it via:
pip install python-dateutil
Or on Ubuntu:
sudo apt-get install python-dateutil
Why didn't someone tell me I was being a total noob? All I had to do was copy the dateutil directory to someplace in my Python path, and it was good to go.
Looks like the setup.py uses easy_install (i.e. setuptools). Just install the setuptools package and you will be all set.
To install setuptools in Python 2.6, see the answer to this question.
Install from the "Unofficial Windows Binaries for Python Extension Packages"
http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-dateutil
Pretty much has every package you would need.
It is a little tricky for people who is not used to command prompt. All you have
to do is open the directory where python is installed (C:\Python27 by default) and open the command prompt there (shift + right click and select open command window here) and then type :
python -m pip install python-dateutil
Hope that helps.
Using setup from distutils.core instead of setuptools in setup.py worked for me, too:
#from setuptools import setup
from distutils.core import setup
If you are offline and have untared the package, you can use command prompt.
Navigate to the untared folder and run:
python setup.py install
Just run command prompt as administrator and type this in.
easy_install python-dateutil
You could also change your PYTHONPATH:
$ python -c 'import dateutil'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named dateutil
$
$ PYTHONPATH="/usr/lib/python2.6/site-packages/python_dateutil-1.5-py2.6.egg":"${PYTHONPATH}"
$ export PYTHONPATH
$ python -c 'import dateutil'
$
Where /usr/lib/python2.6/site-packages/python_dateutil-1.5-py2.6.egg is the place dateutil was installed in my box (centos using sudo yum install python-dateutil15)
First confirm that you have in C:/python##/Lib/Site-packages/ a folder dateutil, perhaps you download it, you should already have pip,matplotlib, six##,,confirm you have installed dateutil by--- go to the cmd, cd /python, you should have a folder /Scripts. cd to Scripts, then type --pip install python-dateutil --
----This applies to windows 7 Ultimate 32bit, Python 3.4------
I followed several suggestions in this list without success. Finally got it installed on Windows using this method: I extracted the zip file and placed the folders under my python27 folder. In a DOS window, I navigated to the installed root folder from extracting the zip file (python-dateutil-2.6.0), then issued this command:
.\python setup.py install
Whammo-bammo it all worked.

Categories