user12428796
user12428796

Reputation:

Importing packages created Python

I have folder structure like below:

TEST- A- a1.py a2.py B- b1.py C- myscript.py

So, how can i import myscript.py in a1.py script without manually setting current working directory in a1, a2 and b1 scripts

Upvotes: 0

Views: 43

Answers (1)

Sreeram TP
Sreeram TP

Reputation: 11927

I assume you want to want to have a folder structure like shown below,

project
│
├───A
│       a1.py
│       a2.py
├───B
│       b.py
├───C
│   │   my_script.py

And you want to import functions or classes from my_script.py to some other file.

To do that you have to add init.py file to folders. Your folder structure will look like this after doing that.

project
|
│   __init__.py
│
├───A
│       a1.py
│       a2.py
│       __init__.py
│
├───B
│       b.py
│       __init__.py
│
├───C
│   │   my_script.py
│   │   __init__.py

You can import my_script like this in any of the folders above,

from project import my_script

Upvotes: 1

Related Questions