0

I'm using visual studio code to build up a python project. It is my first time to build a project with an Editor(in the past using Pycharm).

As the project gets bigger, I want to import some classes I have written in the other directories(in the same project). First I added the __init__.py in the directories I want to import from. However, it did not work and it raised:

ModuleNotFoundError: No module named 'LinkedList'

Then I searched on the internet and found that my project path was not in the sys.path. So I added the following in the file where I want to import class:

sys.path.append('D:\\Python_Projects\\Python_DataStructure')

And it worked. I have a question here. How could I permanently add my project path into the sys.path so I did not need to do the sys.path.append in every file where I want to import some classes or functions from other directories?

My project structure currently looked like this:

Python_DataStructure\
                     LinkedList\
                                 Mylinkedlist.py
                                 __init__.py
                     Queue\
                           queue.py
                           __init__.py

I omitted some python files in the directories above for simplicity

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Osito Wang
  • 13
  • 2
  • It sounds as though you need to look into writing your own pip packages... – Sayse Jun 15 '18 at 05:56
  • perhaps try to create package from your project and install it https://stackoverflow.com/a/41536128/1265980 – Ereli Jun 15 '18 at 06:02
  • your names for the folders are not too canonical, anyone working / helping you with a code would have appreciated changs according to PEP8 – Evgeny Jun 15 '18 at 07:18

1 Answers1

0

You can set the environment variable PYTHONPATH to a colon-separated list of directories to search for imported modules.
You can do that by going into: My Computer > Properties > Advanced System Settings > Environment Variables >
Add the paths to the modules here.

D.Tomov
  • 1,018
  • 2
  • 15
  • 36