Reputation: 1200
This, I presume, is a fairly common question, but I can't seem to import my Python module.
This code runs absolutely fine on my Ubuntu machine back home, but doesn't seem to work on my Windows machine at work. Which leads me to believe it boils down to either a difference in how Python works on Windows compared to a *nix-system, or that it handles modules completely different.
From my understanding modules should be loaded based on the sys.path
, as per every other question regarding the issue and the reading I've done.
However, I've got a simple project like this:
DataStructures
├───.git
└───Python
├───DoublyLinkedListStarter.py
└───LinkedLists
├───Doubly.py
└──────LinkedNodes
└───DoublyLinkedNode.py
If I navigate to Python/
and run python .\DoublyLinkedListStart.py
, I'm getting a module error in Doubly.py
:
Unable to import module LinkedNodes
The code is very basic, it's a Python implementation of a Doubly linked list, where DoublyLinkedListStarter
imports Doubly
which implements the body Linked List, and imports the DoublyLinkedNode
.
DoublyLinkedListStart.py
:
from LinkedList import Doubly
Doubly.py
:
from LinkedNodes import DoublyLinkedNode
Again, this worked absolutely fine on my Ubuntu machine before I pushed it up to git and pulled it down on my windows machine.
Upvotes: 0
Views: 1965
Reputation: 462
Try this:
from LinkedLists.LinkedNodes import DoublyLinkedNode
But this is an issue with the PYTHONPATH system variable. Another way to do it is to add it to the PYTHONPATH system variable:
set PYTHONPATH=%PYTHONPATH%;LinkedLists\LinkedNodes
python .\DoublyLinkedListStart.py
You have to do that every time you open the command line.
The path python is using can be accessed from within a python script with sys.path, which is a list. That gives another way to add import paths:
import sys
sys.path.append("LinkedLists")
from LinkedNodes import DoublyLinkedNode
You got options ;)
Upvotes: 1