Automatically create requirements.txt

前端 未结 7 1655
灰色年华
灰色年华 2020-11-27 09:07

Sometimes I download the python source code from github and don\'t know how to install all the dependencies. If there is no requirements.txt file I

相关标签:
7条回答
  • 2020-11-27 09:18

    best way for Python 3 is:

    pip3 freeze > requirements.txt
    

    it worked for me...

    0 讨论(0)
  • 2020-11-27 09:25

    If you use virtual environment, pip freeze > requirements.txt just fine. IF NOT, pigar will be a good choice for you.

    By the way, I do not ensure it will work with 2.6.

    UPDATE:

    Pipenv or other tools is recommended for improving your development flow.

    For Python 3 use below

    pip3 freeze > requirements.txt
    
    0 讨论(0)
  • 2020-11-27 09:30

    In my case, I use Anaconda, so running the following command from conda terminal inside my environment solved it, and created this requirements txt file for me automatically:

    conda list -e > requirements.txt
    

    This was taken from this Github link pratos/condaenv.txt

    If an error been seen, and you are using anaconda, try to use the .yml option:

    conda env export > <environment-name>.yml
    

    For other person to use the environment...Or if you are creating a new enviroment on other machine: conda env create -f .yml

    .yml option been found here

    0 讨论(0)
  • 2020-11-27 09:31

    You can use the following code to generate a requirements.txt file:

    pip install pipreqs
    
    pipreqs /path/to/project
    

    more info related to pipreqs can be found here.

    Sometimes you come across pip freeze, but this saves all packages in the environment including those that you don't use in your current project.

    0 讨论(0)
  • 2020-11-27 09:33

    If Facing the same issue as mine i.e. not on the virtual environment and wants requirements.txt for a specific project or from the selected folder(includes children) and pipreqs is not supporting.

    You can use :

    import os
    import sys
    from fuzzywuzzy import fuzz
    import subprocess
    
    path = "C:/Users/Username/Desktop/DjangoProjects/restAPItest"
    
    
    files = os.listdir(path)
    pyfiles = []
    for root, dirs, files in os.walk(path):
          for file in files:
            if file.endswith('.py'):
                  pyfiles.append(os.path.join(root, file))
    
    stopWords = ['from', 'import',',','.']
    
    importables = []
    
    for file in pyfiles:
        with open(file) as f:
            content = f.readlines()
    
            for line in content:
                if "import" in line:
                    for sw in stopWords:
                        line = ' '.join(line.split(sw))
    
                    importables.append(line.strip().split(' ')[0])
    
    importables = set(importables)
    
    subprocess.call(f"pip freeze > {path}/requirements.txt", shell=True)
    
    with open(path+'/requirements.txt') as req:
        modules = req.readlines()
        modules = {m.split('=')[0].lower() : m for m in modules}
    
    
    notList = [''.join(i.split('_')) for i in sys.builtin_module_names]+['os']
    
    new_requirements = []
    for req_module in importables:
        try :
            new_requirements.append(modules[req_module])
    
        except KeyError:
            for k,v in modules.items():
                if len(req_module)>1 and req_module not in notList:
                    if fuzz.partial_ratio(req_module,k) > 90:
                        new_requirements.append(modules[k])
    
    new_requirements = [i for i in set(new_requirements)]
    
    new_requirements
    
    with open(path+'/requirements.txt','w') as req:
        req.write(''.join(new_requirements))
    

    P.S: It may have a few additional libraries as it checks on fuzzylogic.

    0 讨论(0)
  • 2020-11-27 09:41

    if you are using PyCharm, when you open or clone the project into the PyCharm it shows an alert and ask you for installing all necessary packages.

    0 讨论(0)
提交回复
热议问题