Correct to import module class python - python

Python project structure:
src/
- package-name/
-- A/
---B/
b1.py
---C/
c1.py
In c1.py, it uses a function defined in b1.py. I try 2 ways:
Method1: from src.package-name.A.B.b1 import b1_func
Method2: from ..B.b1 import b1_func
The import module starts from package-name directory, so src/ will not be visible in the imported module. So Method1 not working when import my own module.
Method2 is not working when run in IDE. ValueError: attempted relative import beyond top-level package
Any suggestions? thanks.

Do you have __init__.py files in A and B? It may be worthwhile to properly import b1_func into B's and then A's init files.
B __init__.py
from .b1_file import b1_func
or whatever
and A __init__.py
from B import b1_func
Then you should be able to import ..b1_func

I change the "Content Root" to the package-name directory in PyCharm and import package-name.B.b1. It works.

follow these steps to import the packages wherever u want
First of all, add __init__.py in all folders
i.e: __init__.py in src and __init__.py in package and __init__.py in A
and __init__.py in B and __init__.py in C.
If u want to import the functions from b1.py in c1.py add these lines in c1.py file.
import sys
sys.path.append(“../”)
#if u want from src folder add ../../
from B.b1 import YourFunctionName

Related

python import class from different folder

my project has folders that are structured like this:
main
-folder 1
-file1.py
-folder 2
-file2.py
both file1 and file2 having classes.
when I try from main.folder1.file1 import class1 it fails, saying "No module named main". What am I doing wrong and how should I import it?
you have to first create the module by including __init__.py
in the root directory which is same in the hierarchy of main folder and also create an __init__.py in other sub folders to make them accessible as modules.
Here is an example structure from the official documentation.Note how at each level there is __init__.py you have to include similarly.
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
your structure can be like below:
main/
__init__.py
folder1/
__init__.py
file1.py
folder2/
__init__.py
file2.py
Then you can append the path to the module at the top level like below.
You can have it in the __init__.py in the root directory.
sys.path.append(path.dirname(path.dirname(path.abspath(file))))
Then try accessing like from folder1.file1 import class1.
This should resolve your problem.
To further understand your problem read about modules and how to include relative imports by referring documentation.
In python you have to declare each folder as a module by adding a file named __init__.py in each directory including the root. This file can be empty.
you can do this if there are one level above.
import sys
sys.path.insert(0,'../folder1')
sys.path.insert(0,'../folder2')
import function1
import function2
....
Once you run the first 3 lines, you are adding those folders to the working directory. You can then import the functions within those files as if they are in the file already.
If the folders are in the same level, do this,
import sys
sys.path.insert(0,'folder1')
sys.path.insert(0,'folder2')
import function1
import function2
....

Not able to import module from other directory in Python 3.7.1

I have a package structured as:
Classes in those packages are named exactly like the file names. Also, init.py has following code
from tableau_util import tableau_util
from sftp_util import sftp_util
from s3_util import s3_util
I have another file e.g. test.py which is outside this folder 'utils'. I want to import those classes into test.py so my code is
from utils.tableau_util import tableau_util
from utils.sftp_util import sftp_util
from utils.s3_util import s3_util
I am still getting the error:
ModuleNotFoundError: No module named 'tableau_util'
What can I try to resolve this?
Without knowing everything I would guess that you are trying to run your test.py as a normal python script. Given this folder structure
.
├── __init__.py
├── test
│   ├── __init__.py
│   └── test.py
└── utils
├── __init__.py
├── s3_util.py
└── tableau_util.py
with these files test.py
from utils.s3_util import s3_util
from utils.tableau_util import tableau_util
s3_util()
tableau_util()
import sys
print(sys.path)
s3_util.py
def s3_util():
print('Im a s3 util!')
tableau_util.py
def tableau_util():
print('Im a tableu util!')
if you just tried to run python test/test.py in the main folder it will give you the ModuleNotFoundError. That's because it sets the ./test folder as the python path and it won't be able to see the utils folder and therefore be able to import it. However if you run it as python -m test.test (note the lack of .py you don't need it when you run it as a module) that will tell python to load it as a module and then it will run correctly with this output:
Im a s3 util!
Im a tableau util!
If you don't want to put the test.py in another folder you can simply keep it in the parent folder of utils and be able to run it in the traditional python test.py and get the same results. Error while finding spec for 'fibo.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__') has some more reading on the matter.
For the record all my __init__.py files are empty and don't import anything and this is normally how they are setup unless you want to specify certain functions that need to be imported when the module is imported automatically.
I used PyCharm's create package option to create folders and files again and it is working now. Here are my new (working) folder structure:
My main script has following lines of code to import those classes:
from utils_pkg import tableau_util
from utils_pkg import s3_util
from utils_pkg import sftp_util
First, inside __init__.py (or in any sub-module that tries to import one of its siblings from the same package) you should add a "relative import" dot to the beginning of the module name, so that it reads:
from .tableau_util import tableau_util
# ^right here
Second, make sure your current working directory is not utils. A good place to start, for testing, might be to cd to the parent directory of utils instead.

import a file from another location - python

There are two folders , each contain a python file .
For example: first_folder contains a.py & second_folder contains b.py
I tried importing b.py in a.py
But i got not Import error.
ImportError: No module named b
Pls help me to solve this. I also tried creating a blank init.py in both folders, but it did not worked.
Folder structure:
/home/user/scripts/
|
|--------python_scripts
| |
| |
| |------- a.py
|
|--------lib
|
|-------b.py
Suppose you have files like this
.
├── first_folder
│ └── a.py
└── second_folder
└── b.py
You can use abs path to import a.py as a module in b.py
import importlib.util
spec = importlib.util.spec_from_file_location('a', 'path/to/first_folder/a.py')
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
print(dir(foo))
There is another convenient way to load b.py
$ cd path/to/second_folder
$ ln -s path/to/first_folder ./first_folder
and import a.py as a normal python module
import a from first_folder
Python's import is going to look for the modules (.py files) in the python path. You can see what is in python path in the sys.path variable.
By default, sys.path will include directory where the script you ran is located. It will also include everything defined in the PYTHONPATH environment variable.
So, if you have two directories with .py files, you can either put both in python path, or you can make sure everything all of your source is under the same path entry.
Option 1
(this syntax depends on your shell, here it is for windows)
set PYTHONPATH=%PYTHONPATH%;\path\to\second_folder
python \path\to\first_folder\a.py
Then, you can simply import b.
Option 2
Create an empty __init__.py in both directories and a run.py in the directory above them, so you have:
root_dir
run.py
first_folder
__init__.py
a.py
sedond_folder
__init__.py
b.py
Make run.py your entry point (run python run.py) and then you can import any of the modules from any other module using their full module names:
import first_folder.a
import second_folder.b
If the files are in the same directory type this
Import .other_file

Import Script from a Parent Directory

How do I import a module(python file) that resides in the parent directory?
Both directories have a __init__.py file in them but I still cannot import a file from the parent directory?
In this folder layout, Script B is attempting to import Script A:
Folder A:
__init__.py
Script A:
Folder B:
__init__.py
Script B(attempting to import Script A)
The following code in Script B doesn't work:
import ../scriptA.py # I get a compile error saying the "." is invalid
You don't import scripts in Python you import modules. Some python modules are also scripts that you can run directly (they do some useful work at a module-level).
In general it is preferable to use absolute imports rather than relative imports.
toplevel_package/
├── __init__.py
├── moduleA.py
└── subpackage
├── __init__.py
└── moduleB.py
In moduleB:
from toplevel_package import moduleA
If you'd like to run moduleB.py as a script then make sure that parent directory for toplevel_package is in your sys.path.
From the docs:
from .. import scriptA
You can do this in packages, but not in scripts you run directly. From the link above:
Note that both explicit and implicit relative imports are based on the
name of the current module. Since the name of the main module is
always "__main__", modules intended for use as the main module of a
Python application should always use absolute imports.
If you create a script that imports A.B.B, you won't receive the ValueError.
If you want to run the script directly, you can:
Add the FolderA's path to the environment variable (PYTHONPATH).
Add the path to sys.path in the your script.
Then:
import module_you_wanted

Python - Doing absolute imports from a subfolder

Basically I'm asking the same question as this guy: How to do relative imports in Python?
But no one gave him a correct answer. Given that you are inside a subfolder and you want to go up a directory and then into ANOTHER subfolder, doing what they suggested does not work (as the OP pointed out in his comments to their answers).
I know that you can do this by using sys.path, but I would prefer a cleaner method.
Example:
App
__init__.py
Package_A
--__init__.py
--Module_A.py
Package_B
--__init__.py
--Module_B.py
How would I import Module_A into Module_B?
main.py
setup.py
app/ ->
__init__.py
package_a/ ->
__init__.py
module_a.py
package_b/ ->
__init__.py
module_b.py
You run python main.py.
main.py does: import app.package_a.module_a
module_a.py does import app.package_b.module_b
Alternatively 2 or 3 could use: from app.package_a import module_a
That will work as long as you have app in your PYTHONPATH. main.py could be anywhere then.
So you write a setup.py to copy (install) the whole app package and subpackages to the target system's python folders, and main.py to target system's script folders.
If I'm reading correctly, in Python 2.5 or higher:
from ..Module_B import Module_B
I thought I was well-versed in Python but I had no idea that was possible in version 2.5.
If you are then importing Module_B in to App, you would
Module_B.py:
import ModuleA
App.py (which also imports ModuleA which is now by default in your Pythonpath)
import Module_B.Module_B
Another alternative, is to update __init__.py (the one in Module_A/App folder) to:
import os
import sys
sys.path.extend('%s../' % os.getcwd())
import ModuleA
Another alternative, is to add your folder to the PYTHONPATH environment var.

Categories