------------------------------
Charles Sasaki-Skopp
IT Coordinator/Developer
VSI
Albany OR US
------------------------------
------------------------------
Charles Sasaki-Skopp
IT Coordinator/Developer
VSI
Albany OR US
------------------------------
Dealing with the Python Path
The Question comes up from time to time on how can a U2 developer have multiple versions of a Python module on the same system.
IMHO: This is not a good idea and should be avoided
There are a lot of situations where you can run into issues with this type of Python implementation, and I would recommend having a different system for development, and not doing anything on the production server.
How Python determine what is version of a package/module is imported
Python has its own path variable, and uses it to determine where it looks when you import a package.
import pprint
import sys
pprint.pprint(sys.path)
['C:\\\\U2\\\\ud82\\\\XDEMO\\\\JUPYTER', 'c:\\\\u2\\\\ud82\\\\python\\\\python39.zip', 'c:\\\\u2\\\\ud82\\\\python\\\\DLLs', 'c:\\\\u2\\\\ud82\\\\python\\\\lib', 'c:\\\\u2\\\\ud82\\\\python', '', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\win32', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\Pythonwin', 'C:\\\\U2\\\\ud82\\\\bin', 'C:\\\\U2\\\\ud82\\\\XDEMO\\\\PP', 'c:\\\\u2\\\\ud82\\\\python\\\\lib\\\\site-packages', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\mrajkowski\\\\.ipython']
Note that in the above you see the C:\\U2\\ud82\\bin and C:\\U2\\ud82\\XDEMO\\PP these are from my u2.pth file that is in my UDTHOME account.
If a package/module is in multiple locations, the first one found would be used. So, if something I wanted was in the current directory as well as being in C:\\U2\\ud82\\XDEMO\\PP, the version in the current directory would be loaded since it was found first.
It is possible to modify the sys.path
sys.path.remove('C:\\\\U2\\\\ud82\\\\XDEMO\\\\PP')
sys.path.insert(0, 'C:\\\\U2\\\\ud82\\\\XDEMO\\\\PP')
pprint.pprint(sys.path)
['C:\\\\U2\\\\ud82\\\\XDEMO\\\\PP', 'C:\\\\U2\\\\ud82\\\\XDEMO\\\\JUPYTER', 'c:\\\\u2\\\\ud82\\\\python\\\\python39.zip', 'c:\\\\u2\\\\ud82\\\\python\\\\DLLs', 'c:\\\\u2\\\\ud82\\\\python\\\\lib', 'c:\\\\u2\\\\ud82\\\\python', '', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\win32', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\Pythonwin', 'C:\\\\U2\\\\ud82\\\\bin', 'c:\\\\u2\\\\ud82\\\\python\\\\lib\\\\site-packages', 'C:\\\\Users\\\\mrajkowski\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\mrajkowski\\\\.ipython']
Note that while this seems like it may do what you want, you run the risk of having issues once you have loaded a package/module.
print("first time you import, the code is executed")
import Greeting
print("if you import something that was previously imported it does not re-import")
import Greeting
first time you import, the code is executed Type your name: mike Hello mike if you import something that was previously imported it does not re-import
This can cause confusion on wether or not your changes are being used. The best practice is to stop the running Python process and start a new one.
How to reload a Python module
Yet, if you are on a test box at the REPL prompt, you might want to reload the module. Look at using the importlib module to help you with this.
import importlib
reloaded = importlib.reload(Greeting)
print()
# as an added bonus, the reload function returns the path to what was importd
print("Reloaded: " + str(reloaded))
# you can also see the by looking at the __file__ property of the module object
print("Greeting.__file__ = " + Greeting.__file__)
Type your name: mike Hello mike Reloaded: <module 'Greeting' from 'C:\\\\U2\\\\ud82\\\\XDEMO\\\\PP\\\\Greeting.py'> Greeting.__file__ = C:\\U2\\ud82\\XDEMO\\PP\\Greeting.py
Looking at the path of the module is a good way to determine what you just imported.
Note there are also ways to unload modules so you can reload them, but once again, I do not recommend doing this since you run the risk of loading the wrong module and/or pointing to the wrong data.
IMHO: This is not a good idea and should be avoided
------------------------------
Mike Rajkowski
support
Rocket Internal - All Brands
DENVER CO US
------------------------------
Sign up
Already have an account? Login
Welcome to the Rocket Forum!
Please log in or register:
Employee Login | Registration Member Login | RegistrationEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.