Is there any way to get the installed modules without version info with pip?

2k views Asked by At

I tried pip freeze. I need it to output just the modules delimited by newlines, like pip freeze, except without the version number, because I am trying to create an auto upgrader, and want to do pip install --upgrade <module> where "<module>" is the name of the module without the version number, because as far as I know, you aren't supposed to provide the version number if you are trying to upgrade a module. For example:

colorama  
Flask  
pywin32    

Instead of what pip freeze would do:

colorama==\<version>  
Flask==\<version>  
pywin32==\<version>    

Where "<version>" is the version.
I looked through the documentation for pip commands or options of pip freeze, but found none. I'm on Windows 10.

2

There are 2 answers

0
wim On BEST ANSWER

For Linux / macOS:

pip freeze | awk -F "==" '{ print $1 }'

The -F is for specifying a custom field separator, and $1 prints the first field.


For Windows, where awk may not be available:

pip freeze | py -c "for p in __import__('sys').stdin: print(p.split('=')[0])"
0
sinoroc On

For a pure Python (3.7+) version:

python -c "import importlib.metadata; print('\n'.join([d.metadata['Name'] for d in importlib.metadata.distributions()]))"

References: