So As you can see in the above pic I like to document what library requirements are needed to run this script.
(mostly for when I move the script to another computer or upload it to someone else)
But my problem is not all imports match the name of there pip install command, key example here being BeautifulSoup.
So I would like to know if there is a python command to look up a library name from it's import.
Like hay, tell me what packadge this import is from kinda thing.
Bonus: As you can see in the project we are using lxml with BeautifulSoup so we don't need to import it, but it does need to be installed. Is there a "thing" that I can just chuck a py script at and it will tell me what requirements I need to run it.
I know that pyinstaller for example will package all the librarys it needs to make the exe, I wonder if there is a way I can run pyinstaller in like an info mode.
Edit: I don't want to build a requirements.txt for the hole computer, just one for the script i'm working on currently.
Or just requirements.txt for only one py file, not the hole computer.
CodePudding user response:
The usual way to solve that is by creating a requirements.txt
file:
pip freeze > requirements.txt
Then you can add it to version control (or simply copy over to another computer) and:
pip install -r requirements.txt
Which will install all the packages listed on the file into the new environment.
CodePudding user response:
The pipreqs
library (from pypi) is used to collect imported libraries for the specific program and export to a requirements.txt
file.
Using pip freeze
exports all installed libraries and is generally not ideal for project based requirements.txt
files.
CodePudding user response:
You should define the requirements for a script to run in a so called requirements file. The search command provided by pip may be what you are looking for. But there is no reliable way to reverse lookup package names just based on their import name. Your modus operandi should be:
- Determine what packages you need
- Add needed packages into
requirements.txt
file - Install the dependencies (preferably within a virtual environment or similar) via
pip install -r requirements.txt
If you put the requirements file into version control along with your script others can profit from your work much easier.
CodePudding user response:
Thank you to S3DEV
pipreqs worked.
pipreqs C:\MyProjectFolder
Will make a requirements.txt for only the python files in the MyProjectFolder folder and only the scrips in that folder, not the hole computer.
It will save the requirements.txt to the same folder.
Also sorry for being new here, but this was a sub comment and I don't know how to "give the points" for solving my issues to S3DEV. So I'll just mark this one as solved...