Why Use Sys.Path.Append(Path) Instead of Sys.Path.Insert(1, Path)

Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules

Because python checks in the directories in sequential order starting at the first directory in sys.path list, till it find the .py file it was looking for.

Ideally, the current directory or the directory of the script is the first always the first element in the list, unless you modify it, like you did. From documentation -

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

So, most probably, you had a .py file with the same name as the module you were trying to import from, in the current directory (where the script was being run from).

Also, a thing to note about ImportErrors , lets say the import error says -
ImportError: No module named main - it doesn't mean the main.py is overwritten, no if that was overwritten we would not be having issues trying to read it. Its some module above this that got overwritten with a .py or some other file.

Example -

My directory structure looks like -

 - test
- shared
- __init__.py
- phtest.py
- testmain.py

Now From testmain.py , I call from shared import phtest , it works fine.

Now lets say I introduce a shared.py in test directory` , example -

 - test
- shared
- __init__.py
- phtest.py
- testmain.py
- shared.py

Now when I try to do from shared import phtest from testmain.py , I will get the error -

ImportError: cannot import name 'phtest'

As you can see above, the file that is causing the issue is shared.py , not phtest.py .

Why use sys.path.append(path) instead of sys.path.insert(1, path)?

If you have multiple versions of a package / module, you need to be using virtualenv (emphasis mine):

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

That's why people consider insert(0, to be wrong -- it's an incomplete, stopgap solution to the problem of managing multiple environments.

Sys.path.insert inserts path to module but imports are not working

Replace the code as this you dont need to add the folder to the path all you need is the path to the folder

import sys
import os

sys.path.insert(0, os.path.abspath('../../'))
import myfolder
print(sys.path)

Why do I need to include sys.path.append to import a module with Python 3.6 and my colleges doesn't need?

Just set the PYTHONPATH environment variable to (the full path to) Myproject. That tells Python where to find modules to import, and it works regardless of which directory you run the script from. This avoids the need to modify sys.path.

import python module using sys.path.append

I'm afraid you can't do it that way.

Because of structure:

/home/sam/pythonModules/module1/lib/module1.py
/home/sam/pythonModules/module2/lib/module2.py

You can't put both:

  • /home/sam/pythonModules/module1 and
  • /home/sam/pythonModules/module2

in sys.path and expect that Python will find:

  • module1 in module1/lib and
  • module2 in module2/lib

when you try import like:

from lib.module1 import A
from lib.module2 import B

If you put /home/sam/pythonModules/module1 before /home/sam/pythonModules/module2 in sys.path array, then import lib.MODULE will search for MODULE in /home/sam/pythonModules/module1/lib.

Since there is only module1 and no module2 in it, you get error.

What you can do is to put both

  • /home/sam/pythonModules/module1/lib/ and
  • /home/sam/pythonModules/module2/lib/

in sys.path and expect Python to correctly import them with next lines:

from module1 import A
from module2 import B

sys.path.insert cannnot import other python files

The sys.path.insert() command inserts a path into the system path and should not contain the filename.

Please try below using your structure:

little main.py:

import sys
import sample_tree1

sys.path.insert(0, r'/my/absolute/path/5.0.8/treetwo')
print(sys.path) # view the path and verify your insert

import sample_tree2

print(sample_tree2.tree2_func())

sample_tree2.py in treetwo

def tree2_func():
return 'called tree2'

Outputs:

['/my/absolute/path/5.0.8/treetwo', '... other paths']

called tree2



Related Topics



Leave a reply



Submit