Does anyone know why Import["http://**server**/file.m"] can't make a change to $Path? It seems like file.m can set pretty much any other variable, but not $Path.

I suspect Get[] would work, but I can't seem to get Get[] to fetch files over the web.

  • 1
    $\begingroup$ Possibly a security measure? $\endgroup$ – Mr.Wizard Jan 17 '13 at 7:59
  • $\begingroup$ I'm creating and deleting files and directories in my Imported file, so if it's a security measure it's impressively poorly designed. It feels more like there's some "specialness" to the context that the Import is being evaluated in. $\endgroup$ – sblom Jan 17 '13 at 15:37
  • 1
    $\begingroup$ In V9, we made Get take an InputStream, which can be opened from a URL. In reference.wolfram.com/mathematica/ref/Get.html, and where it says "Updated in 9" in the upper right, click "Show changes". $\endgroup$ – Joel Klein Jan 17 '13 at 15:48
  • $\begingroup$ Actually, in V9, Get from a URL string should just work, you don't need to explicitly open a stream. $\endgroup$ – Joel Klein Jan 17 '13 at 16:32

I think \$Path is probably being set in a Block in the Import call. I put this sample code on a server in test.m:

beforePath = $Path;
    AppendTo[$Path, "d:/tomcat-6.0.35"];
afterPath = $Path;

I can see that afterPath has the correct value, but $Path is unmodified.

In[14]:= Import["http://jfkleinlx.wri.wolfram.com/test.m"]

Out[14]= "Thu 17 Jan 2013 09:58:52"

In[19]:= Complement[afterPath, beforePath]

Out[19]= {"d:/tomcat-6.0.35"}

In[20]:= $Path === beforePath

Out[20]= True

With V9, to work around this you can open a stream to a URL and use Get as you suspected:

In[21]:= st = OpenRead["http://jfkleinlx.wri.wolfram.com/test.m"]

Out[21]= InputStream["http://jfkleinlx.wri.wolfram.com/test.m", 133]

In[22]:= Get[st]

Out[22]= "Thu 17 Jan 2013 10:06:11"

In[24]:= $Path === afterPath

Out[24]= True

Remember to Close the stream. Or, you can just Get the URL directly: Get["http://jfkleinlx.wri.wolfram.com/test.m"], which opens a stream internally.

  • 1
    $\begingroup$ That is a lame test.m. You should instead replace the contents with SystemOpen["https://www.youtube.com/watch?v=dQw4w9WgXcQ"] :D $\endgroup$ – rm -rf Jan 17 '13 at 18:08
  • $\begingroup$ That's a good point, here's a stopgap if you are working without Internet: Speak[Uncompress[ "1:eJxVjcENAjEMBFPK0sCVwgMq8BErRAT75IsPpS8KJArwyMPSamc1Pq16ub5DCGc+2JA0S0JVpHwwmjp8W/BnIoTCdfRRXzITcwGZukRQv8g729jOsyc9vuabtZns1HrSuDaeQeVS0F9nHua7/7wfYipCUA=="]] $\endgroup$ – Joel Klein Jan 17 '13 at 20:16
  • $\begingroup$ There's no way to ninja out of a block, right? $\endgroup$ – sblom Jan 19 '13 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.