Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: import symbolic link of a folder

I have a folder A which contains some Python files and __init__.py.

If I copy the whole folder A into some other folder B and create there a file with "import A", it works. But now I remove the folder and move in a symbolic link to the original folder. Now it doesn't work, saying "No module named foo". Does anyone know how to use symlink for importing?

like image 468
Ecir Hana Avatar asked Jan 05 '12 20:01

Ecir Hana


People also ask

Can you symbolic link a directory?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows. Some users refer to symlinks as soft-links.

Can you import folders in Python?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How do I create a symbolic link from one directory to another?

Ln Command to Create Symbolic Links By default, the ln command creates a hard link. Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to.

How do I import a python path?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.


2 Answers

Python doesn't check if your file is a symlink or not! Your problem lies probably in renaming the modules or not having them in your search-path!

If ModuleA becomes ModuleB and you try to import ModuleA it can't find it, because it doesn't exist.

If you moved ModuleA into another directory and you generate a symlink with another name, which represents a new directory, this new directory must be the common parent directory of your script and your module, or the symlink directory must be in the search path.

BTW it's not clear if you mean module or package. The directory containing the __init__.py file becomes a package of all files with the extension .py (= modules) residing therein.

Example

DIRA
  + __init__.py    <-- makes DIRA to package DIRA
  + moduleA.py     <-- module DIRA.moduleA

Moving and symlink

/otherplace/DIRA  <-+
                    |  points to DIRA
mylibraries/SYMA  --+  symbolic link

If SYMA has the same name as DIRA and your script is in the directory SYMA then it should just work fine. If not, then you have to:

import sys
sys.path.append('/path/to/your/package/root')

If you want to import a module from your package SYMA you must:

import SYMA.ModuleA

A simple:

import SYMA

will import the packagename, but not the modules in the package into your namespace!

like image 120
Don Question Avatar answered Sep 22 '22 15:09

Don Question


This kind of behavior can happen if your symbolic links are not set up right. For example, if you created them using relative file paths. In this case the symlinks would be created without error but would not point anywhere meaningful.

If this could be the cause of the error, use the full path to create the links and check that they are correct by lsing the link and observing the expected directory contents.

like image 36
CJ Ryan Avatar answered Sep 23 '22 15:09

CJ Ryan