I want to work with the mercurial repository of hg itself. That is, I cloned Mercurial from https://www.mercurial-scm.org/repo/hg and want to run some hg commands inside the cloned repository. The problem is that when running hg inside this clone hg executable tries to load its python modules from this directory and not from /usr/lib/pythonVERSION etc. As I understand it this happens because Python import path sys.path contains an empty string as first entry which probably means "current directory". PYTHONPATH environment variable is not set.
The questtion is how can I prevent my installed hg from importing "wrong" modules.
os. remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
To undo the sys. path. append , you just need to remove that line from your script. Since the path is only modified for your current script and not system wide until you edit the PYTHONPATH .
To find out which directory in python you are currently in, use the getcwd() method. Cwd is for current working directory in python. This returns the path of the current python directory as a string in Python. To get it as a bytes object, we use the method getcwdb().
The way I would deal with the topic is by creating a /usr/local/bin/hg sh script with the following contents:
#!/bin/sh
PYTHONPATH=/usr/lib/pythonVERSION/site-packages /usr/bin/hg
(Ubuntu-based distributives use dist-packages instead of site-packages)
PYTHONPATH is a special environment variable respected by Python interpreter to get extra module import paths.
Alternatively, you can export PYTHONPATH into your shell, but it will affect your whole experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With