Search code examples
pythonimporterror

Some packages can be imported others cannot. Why?


The packages I installed work perfectly in Jupyter Notebooks but when I try importing the same packages in Sublime Text I get the following error message:

ImportError: No module named flask

Interestingly, some packages (like matplotlib and numpy) import with no problem but some (like flask and networkx) do not. I am currently on Python Version 3.7.6 and I have tried sudo & pip3 install.
Anyone have any idea what is happening?


Solution

  • That sounds like you've installed those packages in the environment in which Jupyter is running, but not in the environment where Sublime Text is running.

    In order to be able to use different combinations of packages on different projects, python packages are often installed into an environment (often "virtual environment", though there can be others). It sounds like flask and networkx were installed that way, so you only get them from your Jupiter "project" but not your Sublime Text "project". The matplotlib and numpy libraries are probably installed by default.

    Two solutions, depending on the circumstances:

    • Change the configuration in Jupyter and/or Sublime Text so they use the same environment (so they're the same project). This is most practical if you have them both running on the same computer, because you'll be sure of having the same versions of the same packages used in both places.

    OR

    • Repeat the installation of flask and networkx in the Sublime Text environment. This will mean they're separate projects, but may be most practical if you're unsure of the setup, or if Jupyter is running on a different computer than Sublime Text. You'll have to make sure you match any package upgrades or installations in the future.