5

Is there a way to get python to read modules from a network?

We have many machines and it would be a too much effort to update each machine manually each time I change a module so I want python to get the modules from a location on the network.

Any ideas?

Jared
  • 81
  • 1
  • 2
  • 6
  • possible duplicate of [Dynamically importing Python module](http://stackoverflow.com/questions/3799545/dynamically-importing-python-module) – Piotr Dobrogost Jan 22 '12 at 21:07

6 Answers6

4

Mount your network location into your file-system and add that path to your PYTHONPATH. That way, Python on your local machine will be able to see the modules which are present in the remote location. You cannot directly import from modules remotely, like specifying a js file in html.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • What do you mean 'like specifying a js file in html'.. like a – ryanmonk Oct 26 '16 at 13:28
  • @ryanmonk in HTML you can import modules even though they are not present in project folder. you import it from internet as follows. but python does not allow it. – Mangesh Divate Mar 09 '22 at 06:57
2
sys.path.append(r'\\network\path')
import module
ryanmonk
  • 171
  • 1
  • 7
2

How I ended up doing this:

Control Panel\All Control Panel Items\System >> Advanced >> Environment Variables >> System Variables >> New >> Name = PYTHONPATH, value = \server\scriptFolder

Thanks everyone for all the help :)

Jared
  • 81
  • 1
  • 2
  • 6
2

It might be notable that a module for importing packages/modules available through HTTP/S exists and it is httpimport. This is for both Python2 and Python3.

So, as of the accepted answer, it turns out that there are ways to programmatically import remote modules "like javascript" as follows:

>>> with httpimport.remote_repo(['package1'], 'http://my-codes.example.com/python_packages'):
...     import package1
...
>>> # -- 'package1' code is available here --

Edit (31/01/2023): The syntax of most httpimport commands has changed after the 1.0.0 re-write. The new parameters for remote_repo omits the first argument, as below:

>>> with httpimport.remote_repo('http://my-codes.example.com/python_packages'):
...     import package1
...

You might want to look at all usage examples provided in the repository README: https://github.com/operatorequals/httpimport#basic-usage

operatorequals
  • 381
  • 2
  • 5
1

I believe you're looking for a distributed computing framework, where you deploy code and data to one node and they are distributed as task among a cluster of clients/servers/peers. Check Pyro, execnet, Parallel Python, Jug and RPyC.

TryPyPy
  • 6,214
  • 5
  • 35
  • 63
0

While it's a little pathological to want to import modules over the network, it is actually possible. Take a look at the source for zipimport to get an idea of how it can be done.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Could you comment in a few words why this is not a good idea? Is this a disponibility concern or something more related to the core language? – WoJ Jul 11 '16 at 08:21
  • Security and speed reasons. If your network is compromised (or if you're doing something even crazier such as importing over the *Internet*) then you risk losing the server. And if you run the script multiple times per second then you will have that much more traffic over the network. – Ignacio Vazquez-Abrams Jul 11 '16 at 08:25