osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Modules


On Wednesday, March 21, 2018 at 7:49:42 PM UTC-5, Jacques Bikoundou wrote:
> It said: ImportError: no module named 'speedml'

I see. And did you check the search path[1] to ensure that
the modules you want to import are indeed located in a
directory which python normally searches?

As an academic excercise, and to better understand the the
Python import mechanism, let us imagine that Python is house
keeping robot. And we shall dub her "Rosie5000".

And you, being the master programmer of "Rosie5000, the
housekeeping robot", you have (wisely) programed Rosie to
search in specific locations of your home for items that you
request (aka: "import"). And being that you knew ahead of
time that you would be asking for food and drinks on a
regular basis, the first place you programmed Rosie to
search is in the kitchen (aka: "stdlib"). But then one day,
an old college roomie drops by for an unexpected visit, and
of course, his car is running low on oil (big surprise,
right?). So you ask Rosie to fetch a can of oil: "import
motoroil" you say, and immediately -- being that Rosie is a
mindless bucket of bolts; and being that she has only been
programmed to search in the kitchen; and simultaneously,
being, that she is oblivious to the fact that kitchens are
hardly ever the proper storage location for cans of nasty
motor oil -- Rosie goes rummaging through the cupboards,
systematically searching for a can of "dead-dino-goo", only to
come back empty handed ("Bad Rosie!" you say, "Bad!"). But
then compassion smacks you, and you say: "Oops, i forgot to
program Rosie to search in the garage, my Bad!"

Yes, "You bad" is correct!

The moral of our little story is that Python, much like your
beloved Rosie5000, will only look where you tell it to look.

Now, the Python gods have been kind enough to preprogram
some default search locations, but not all scripts are
automatically installed in these default locations (nor
should they be!). So if your script exists *OUTSIDE* of the
Python search path, you have two options (and possibly
more) to choose from:

    (1) Move the script into a directory that is _already_
    included in the search path (see: sys.path)

    (2) Add the script's containing directory to the Python
    search path manually (but don't do it by appending to
    sys.path, that's bad juju!)

The first case is just a matter of cutting and pasting the
script in your file browser (aka: points and clicks). But
the second is a little more complicated and can be achieved
in many ways, some of which are OS specific (requiring
voodoo on the command line!). See the following links for
more detail (not exhaustive).

    (NOTE: These links point to Python2.x documentation, but if
    you're using a different version, be sure to choose your
    version from the little drop- down box at the top of the
    page so that you see the most relevent info)

    https://docs.python.org/2/library/sys.html#sys.path
    https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
    https://docs.python.org/2/library/site.html?highlight=pth

--
[1] import sys; print(sys.path)