0

So I have a python script located on my desktop as well as a package I made called testpackage. It contains three scripts each containing one function which prints a word. Here is a sample of one of the scripts.

def dog():
    print ("Dog")

I also have an __init__.py file and contains the following.

import sys, os
pathVar = os.path.normpath('/Desktop/testpackage')
from cat import *
from dog import *
from horse import *

The file on my desktop, named myFile.py, contains the following

from testpackage import dog
dog.dog()

When I run this script, I get an error along the lines of

File 'myFile.py", line1, in from testpackage import dog. File "C:\Users\User\Deskop\testpackage__init__.py", line 3 in from cat import * ModuleNotFoundError: No Module named 'cat'

I have tried every trick under the sun that i've found on google so I'm wondering why it can't locate the script from the __init__ file.

  • 1
    does it answer your question: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – rrrttt Feb 13 '20 at 19:57

2 Answers2

0

I figured it out, I wasnt using the correct command to insert the path.

0

You should put the module's parent dir in PYTHONPATH.

You can achieve this using the following code:

import sys
module_dir = r"/path/to/module/parent/dir"  
sys.path.insert(0, module_dir)
# assuming module is named my_mod.py
import my_mod
Hrisimir Dakov
  • 557
  • 3
  • 9