Python Forum
problem in import module from other folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in import module from other folder
#1
hi
I typed this thread before, but my net was disconnected and I lost it.
by the pic : https://drive.google.com/file/d/1Vwh-88B...drive_link
I wanted to import test2.py into goal.py.
D is my D drive on my PC. Because the names of folders and files are long I aliased them to simple letters.
I read in the web some suggestions for address:https://stackoverflow.com/questions/4383...ent-folder
and tried to do them, but after running goal.py in cmd each time, I received an error message like below :
Error:
D:\..._module\Scripts>python.exe goal.py Traceback (most recent call last): File "D:\..._module\Scripts\goal.py", line 4, i n <module> from ... import * ModuleNotFoundError: No module named 'test'
Explanation: As I said before I aliased the names of files and folders for simplicity. the ... in the above error is what I replaced with real names. the real goal file on my PC has several commented lines code which are the suggestions on the above site, but could not solve my problem and the file is very bad and messy, so I did not type (copy) here.
my question:
  1. By attention to the pic, how can import test1 into goal.py
    if __init__ .py in folder1 is nessary or not?
    If folder1 is a virtual env(as it is for mine), is the answer to the first question different or not?
This problem consume very time of me(one day!).
Any guidance is appreciated.
Reply
#2
The first document to read attentively to understand how Python imports modules is this one

Python basically import modules from directories that are on the modules search path. To see the modules search path, try
>>> import sys
>>> print(sys.path)
sys.path is a list of system paths to folders. If you want to write
from test import test2
then the directory containing test.py needs to be on the modules search path.

For example it will work if you write in goal.py
import sys
sys.path.append('D:/a/b/c/e/f/g/folder1')
from test import test2
Once you master this, you'll learn how to modify sys.path in a persistent way.
akbarza likes this post
Reply
#3
hi Gribouillis
I used your guidance, but again I encountered an error. then I added a r before the address of folder1, namely :
sys.path.append(r"d:\a\b\...\folder1")

then the problem was solved.
thanks again
Reply
#4
Use forward slashes "/" instead of backslashes. Windows works with either and there is no worry about accident escape sequences.
Reply
#5
I have a few home-made "modules" which do things for me. I keep them all in /home/pedro/myPython/myModules/

If I need one of them, I start my Python program like this:

#! /usr/bin/python3
import sys, os, glob
sys.path.append('/home/pedro/myPython/myModules/')
import score_oralsOMR
Then my modules are available. I don't add the path permanently to $PATH because I don't use them very frequently.

Not sure if this is good practice, but it works for me!
Reply
#6
(Sep-01-2023, 05:47 AM)Pedroski55 Wrote: I don't add the path permanently to $PATH because I don't use them very frequently.
Dont mistake the Module Search Path with the environment variable $PATH, they are completely different things.

What you could do is put them in a package that would be permanently available for example
Output:
myPython └── packages └── infrequent ├── __init__.py # add this file to make infrequent a package (an empty file suffices) ├── score_oralsOMR.py └── ... others.py
Then you permanently add '/home/pedro/myPython/packages/' to sys.path by editing your usercustomize.py module for example, then you can write in your scripts
from infrequent import score_oralsOMR
As for module usercustomize, if it doesn't already exist, create usercustomize.py in the directory returned by the command
Output:
python -c "import site; print(site.getusersitepackages())"
In usercustomize.py, add the lines
import site
site.addsitedir('/home/pedro/myPython/packages')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 375 Mar-04-2024, 10:15 AM
Last Post: snippsat
  problem using coloeama module akbarza 3 582 Jan-08-2024, 07:31 AM
Last Post: akbarza
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 564 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  is import cointegration_analysis a recognized module mitcht33 1 437 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in using subprocess module akbarza 5 1,042 Sep-24-2023, 02:02 PM
Last Post: snippsat
  problem in using pyqrcode module to create QRcode akbarza 9 2,000 Aug-23-2023, 04:17 PM
Last Post: snippsat
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 724 Aug-06-2023, 01:09 AM
Last Post: aupres
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 1,731 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 1,019 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  import module error tantony 5 3,464 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020