Importing module from network
Question:
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?
Answers:
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.
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.
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.
How I ended up doing this:
Control PanelAll Control Panel ItemsSystem >> Advanced >> Environment Variables >> System Variables >> New >> Name = PYTHONPATH, value = serverscriptFolder
Thanks everyone for all the help 🙂
sys.path.append(r'\networkpath')
import module
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
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?
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.
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.
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.
How I ended up doing this:
Control PanelAll Control Panel ItemsSystem >> Advanced >> Environment Variables >> System Variables >> New >> Name = PYTHONPATH, value = serverscriptFolder
Thanks everyone for all the help 🙂
sys.path.append(r'\networkpath')
import module
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