Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Python update module install dir after moving folder?

Tags:

python

I installed a Python module from GitHub. Here is an example of the commands I ran:

cd ~/modules/
git clone https://github.com/user/xyz
cd xyz
python setup.py develop

this installed the the module successfully in the current folder. Then from some other folder, I did:

cd ~/test
python -c 'import inspect; import xyz; print(inspect.getfile(xyz))'

which gave the following output:

/home/hakon/modules/xyz/xyz/__init__.py

Now, I decided I wanted to move the install folder. For example,

cd ~/modules/xyz
mv xyz xyz2

But now Python cannot find the module any longer. For example:

cd ~/test
python -c 'import inspect; import xyz; print(inspect.getfile(xyz))'

With output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'xyz'

Questions:

  • Where does Python register the location of modules?
  • How can I update the registry to the new location of the module?
like image 220
Håkon Hægland Avatar asked Feb 02 '26 00:02

Håkon Hægland


1 Answers

According to the documentation for setup tools develop command:

The develop command works by creating an .egg-link file (named for the project) in the given staging area. If the staging area is Python’s site-packages directory, it also updates an easy-install.pth file so that the project is on sys.path by default for all programs run using that Python installation.

So to answer the question to update the location after you have moved the project folder you can rerun the develop command (from the new folder):

python setup.py develop

Note: you could also unregister the project before you moved it:

python setup.py develop --unistall

but this is not necessary if you just want to update the location.

like image 176
Håkon Hægland Avatar answered Feb 03 '26 14:02

Håkon Hægland