Importing a module from a non-cwd

John Gordon gordon at panix.com
Mon Oct 24 16:38:34 EDT 2011


In <4ea5c1da$0$3708$426a74cc at news.free.fr> candide <candide at free.invalid> writes:

> For instance, suppose I have a file, say my_file.py, located in the cwd, 
> say /home/candide/ and suppose the module to be imported, say 
> my_module.py, is located in the /home/candide/foo/ directory.

> How my_file.py can import the my_module.py module ?

If PYTHONPATH and/or sys.path contain /home/candide/foo/, then you should
be able to:

  import my_module

Or, if foo/ is a real module (i.e. it contains an __init__.py file), this
should work:

  import foo.my_module

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list