Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove current directory from python import path

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.

like image 342
ragol Avatar asked Aug 27 '14 17:08

ragol


People also ask

How do I remove a path in Python?

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.

How do I undo SYS path append?

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 .

How do I find my working directory in Python?

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().


1 Answers

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.

like image 187
Vlad Frolov Avatar answered Oct 12 '22 23:10

Vlad Frolov