How to remove current directory from python import path

I want to work with the Mercurial hg repository. 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 you run hg inside this hg clone, the executable tries to load its python modules from this directory, and not from /usr/lib/pythonVERSION , etc. As I understand it, this is because the Python sys.path import sys.path contains an empty string as the first entry, which probably means "current directory". PYTHONPATH environment variable not set.

Quest: how can I prevent the installation of hg installed hg "wrong" modules.

+6
source share
2 answers

How will I deal with this topic by creating /usr/local/bin/hg sh script with the following contents:

 #!/bin/sh PYTHONPATH=/usr/lib/pythonVERSION/site-packages /usr/bin/hg 

(Ubuntu-based distributions use dist-packages instead of site-packages )

PYTHONPATH is a special environment variable that the Python interpreter interprets to provide additional import paths for modules.

Alternatively, you can export PYTHONPATH to your shell, but this will affect your entire experience.

+1
source

@ragol, I think Padraic has the right solution. Inside the python script that you are trying to run the hg commands, you need to include the following command: sys.path.insert(0,"/usr/lib/pythonVERSION")

Put the command at the very beginning of your python script. The command tells python to search in the /usr/lib/pythonVERSION when importing modules.

If this does not work, you may need a more specific path. For example, if the module you are trying to import is located in the /usr/lib/pythonVERSION/site-packages/hg directory, you can use the following command: sys.path.insert(0,"/usr/lib/pythonVERSION/site-packages/hg")

0
source

Source: https://habr.com/ru/post/974428/


All Articles