{"id":42,"date":"2007-12-19T20:33:02","date_gmt":"2007-12-19T22:33:02","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=42"},"modified":"2007-12-19T20:33:02","modified_gmt":"2007-12-19T22:33:02","slug":"let-me-import","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=42","title":{"rendered":"Let Me Import"},"content":{"rendered":"<p>The problem is that you cannot import modules from different (relative) paths in Python. Now sometimes it&#8217;s really a must. Like when you have a generic CPU module and then a subdirectory called &#8216;x86&#8217; that wants to derive that CPU class and create a &#8216;x86CPU&#8217;.<\/p>\n<p>So I tried to mess up with __import__ but without any luck. Then I said I just want to solve this problem, don&#8217;t care how far (ugly) I go. I started with execfile, which AFAIK isn&#8217;t exactly like import, but it&#8217;s good enough for me. execfile(&#8220;..\/cpu.py&#8221;) didn&#8217;t work, unforetunately. I then realized that the cpu.py file imports another file from <em>its own<strong> <\/strong><\/em>directory and since the execfile doesn&#8217;t do some magic with the directories the import simply failed. Bammer. Next thing I did was to add the path of the CPU module to the sys.path and retried my attempt with a success. Although it works, I don&#8217;t like the directory separator which isn&#8217;t cross-platform. And yes I said it&#8217;s a temporary solution, but I&#8217;m still sensitive to it. Usually temporary hacks that work tend to stay there, as much as I don&#8217;t like this reproach&#8230;this is how things work usually, no?<\/p>\n<p>So that one was solved, quite easily, but another one arised. In the original code (when all files used to be in the same directory) when import worked I always import the module name, and not the lot of it (<strike>import * from cpu<\/strike> &#8211; bad habit). So all my code is accessing the CPU module with &#8216;CPU.&#8217; prefix, which is good, you know the source of everything you touch. The new problem is that since I moved to use execfile this prefix is ill now and I must get rid off it.<\/p>\n<p>I thought about changing the globals dictionary to the name of the module I want to execfile() and then switch it back. But it becomes too nasty, and I&#8217;m not sure whether it would work anyway. My first attempt might be ok for some of you, alas, it&#8217;s still not good enough for me.<\/p>\n<p>And I think my design with the files is ok, after all it makes sense. And yes, I know I (maybe?) should have put the files in site-packages directory and the import then would work well. However, I want the files to be in a specific directory out of Python reach (in a way).<\/p>\n<p>Oh by the way, the code of my attempt is:<\/p>\n<p>import os, sys<br \/>\nsys.path.append(os.getcwd()[:os.getcwd().rfind(os.sep)])<br \/>\nexecfile(&#8220;..&#8221; + os.sep + &#8220;cpu.py&#8221;)<\/p>\n<p>Ugly something, heh?<\/p>\n<p>Ok I was just going to publish this post but I had to try another thing which really solved the issue. I managed to use __import__ well.<\/p>\n<p>So now it looks like:<\/p>\n<p>import os, sys<br \/>\nsys.path.append(os.getcwd()[:os.getcwd().rfind(os.sep)])<br \/>\ncpu\u00a0= __import__(&#8220;cpu&#8221;)<\/p>\n<p>This time it solves the second problem as well &#8211; the namespace (of the module)\u00a0 issue. And yet we need the new path. Can we get rid off it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem is that you cannot import modules from different (relative) paths in Python. Now sometimes it&#8217;s really a must. Like when you have a generic CPU module and then a subdirectory called &#8216;x86&#8217; that wants to derive that CPU class and create a &#8216;x86CPU&#8217;. So I tried to mess up with __import__ but without [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[9],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-G","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}