7

I need to create a conda environment file from the imports that I am using in my python source code. For the sake of simplicity let's say that this is how the imports in my file look like:

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

Now I would like to get an env.yml file that has the latest numpy and pytorch defined as imports.

Is there a way to get this done with a oneliner? Or do I have to do it manually? I couldn't find anything relevant in the conda docs.

David
  • 2,109
  • 1
  • 22
  • 27
  • 1
    This is most likely not possible with any conda command. For one, there is no way to match import names with conda packages (they don't have to agree neccessarily, take `PIL`\`Pillow` as an example). Maybe you could try some regex search in your python files, but that would be a very fragile solution – FlyingTeller Nov 22 '19 at 19:41
  • Possibly related: [How to list imported modules?](https://stackoverflow.com/q/4858100/570918) and [Is there a sessionInfo equivalent in Python?](https://stackoverflow.com/a/31906216/570918), where the latter has a minor comment about comparing against `pip freeze` or `conda list`. Overall, though, I agree with @FlyingTeller that it's not a solved problem, particularly because import names don't map one-to-one to package names. – merv Nov 22 '19 at 19:56

0 Answers0