0

I have written a module and stored it in a location say x and then I am running a program from a different location y. Now, I want to import that module in x. How can I do that?

I have tried doing the following before importing but it didn't work for scripts -

os.chdir(x)

I want to utilise only the default modules available in Python 3.8.1.

3
  • 1
    Could you update your question to show the directory structure you are working with? Commented Apr 1, 2020 at 14:12
  • 1
    already answered here Commented Apr 1, 2020 at 14:13
  • 2
    Does this answer your question? Importing files from different folder Commented Apr 1, 2020 at 14:13

1 Answer 1

1

Actually it is answered in the following link:

Importing files from different folder

I will try to summarise that: lets say your folder structure is like this

C:\Users\your_name\common_folder\ x , y the folders x and y lie in the common_folder and let's assume you have python files x1.py inside x folder and y1.py inside y folder. So inside our y1 python file we might be writing something like this x1

import sys 
path = r'C:\Users\your_name\common_folder\x'
sys.path.insert(1, path)
import x1 

then you can use any attribute inside the x python file and run

Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.