Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • online_ts_bkup.sql Oracle
  • Sample WW22 listener.ora Oracle
  • findx.sql /* Find Indexes on specified USER.TABLE_NAME */ Oracle
  • get_vmstat.ksh Linux/Unix
  • FGA Part-I Oracle
  • All Hints for Oracle Databases Oracle
  • Korn Shell Arithmatic Linux/Unix
  • remove archfiles only when it is applied to DR rm_archfiles.sh Linux/Unix
  • Rollback force for distributed transactions Oracle
  • before_trunc.sql Before Truncate table needs to execute following: Oracle
  • Very Good Oralce Internal Tuning Book Oracle
  • Rename Oracle Instance Name Oracle
  • fuser to check who is using diretory Linux/Unix
  • get_ratio.sql get the ratio of users from v$session and this uses CASE-WHEN-THEN clause Oracle
  • Read CSV File using Python Python/PySpark

Python class import from different folders

Posted on 22-Sep-202422-Sep-2024 By Admin No Comments on Python class import from different folders

I have following directory structure. 

root-folder

– config-folder 

json-file.config

– class-folder 

classfile.py

– tests

pytest-file1.py

Now, I need to use a class named myclass from classfile.py in my test/pytestfile1.py. How can I import that class in my pytest file??

To import the myclass from classfile.py into your pytest-file1.py, you can use a relative import assuming your Python environment treats the root-folder as the top-level package. Here’s how you can do it:

1. Relative Import

In your pytest-file1.py, use the following import statement:

from ..class_folder.classfile import myclass

This assumes that:

classfile.py contains myclass.

You are running the tests from the root-folder level (or using a testing framework like pytest that manages imports correctly).

2. Absolute Import (from the root folder)

If you’re running pytest from the root-folder, you can use an absolute import like this:

from class_folder.classfile import myclass

3.Ensure root-folder is in PYTHONPATH

In Python, the PYTHONPATH is an environment variable that specifies which directories Python should search for modules when importing. By default, Python searches for modules in the standard library and in the current working directory. However, sometimes you need to tell Python where to look, especially when dealing with custom directory structures like yours.

Why is PYTHONPATH important?

Your directory structure looks like this:

root-folder

    – config-folder 

        json-file.config

    – class-folder 

        classfile.py  (contains ‘myclass’)

    – tests

        pytest-file1.py

To import myclass from classfile.py in pytest-file1.py, Python needs to know where the class_folder is located. If the root-folder is not in the PYTHONPATH, Python won’t be able to find class_folder or classfile, leading to an ImportError.

How to set PYTHONPATH

1. Temporary way using the command line (recommended for testing)

Before running your tests, you can set PYTHONPATH to include the root-folder so that Python can find the class_folder.

– On Linux/macOS, run:

PYTHONPATH=. pytest

Here, . refers to the current directory (root-folder). This temporarily sets PYTHONPATH to the current directory for that session, allowing Python to find class_folder and any other modules under root-folder.

– On Windows, the equivalent command is:

set PYTHONPATH=. && pytest

This sets PYTHONPATH for the current terminal session.

2. Permanent way by setting it in your environment

You can add the root-folder to PYTHONPATH permanently by configuring your system environment variables.

– On Linux/macOS (in your .bashrc, .zshrc, etc.):

export PYTHONPATH=/path/to/root-folder:$PYTHONPATH

This appends root-folder to the existing PYTHONPATH. After saving, run:

source ~/.bashrc  # or source ~/.zshrc

– On Windows:

Open the System Properties (right-click “This PC” or “Computer”, then choose “Properties”).

Go to Advanced system settings > Environment Variables.

Under System Variables, find the PYTHONPATH variable (create it if it doesn’t exist).

Add the path to root-folder in the value.

This method ensures that every time you run Python, root-folder is part of its module search path.”

What happens after setting PYTHONPATH?

After setting PYTHONPATH to root-folder, you can use an absolute import in your pytest-file1.py:

from class_folder.classfile import myclass

Now, when Python sees this import statement, it knows to look in the root-folder for the class_folder and then import classfile.py.

Python/PySpark

Post navigation

Previous Post: Transfer SQL Profiles from One database to other database.
Next Post: Reading config file from other folder inside class

Related Posts

  • Add new columns in dataframe Python/PySpark
  • Reading config file from other folder inside class Python/PySpark
  • How to connect to Oracle Database with Wallet with Python. Oracle
  • Read CSV file using PySpark Python/PySpark
  • Getting started with notebook Python/PySpark
  • Read CSV File using Python Python/PySpark

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • AMC10 (0)
  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (150)
  • MYSQL (5)
  • Oracle (403)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (151)
  • SQL scripts (349)
  • SQL Server (6)
  • Uncategorized (9)
  • Videos (0)

Recent Posts

  • Tutornet.US — Source of excellent learning14-Oct-2026
  • OCI Infrastructure Patching14-Oct-2026
  • Free API credits for AI models14-Oct-2026
  • AMC1020-Sep-2026
  • Key Management in Oracle: The Core Issue: Missing Master Key12-May-2026
  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sh23-Apr-2026
  • Running PDB on single node in RAC09-Apr-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • get_vmstat_solaris Oracle
  • V$CONTROLFILE_RECORD_SECTION reference notes. Oracle
  • RMAN : Consistent Backup, Restore and Recovery using RMAN Oracle
  • Good Oracle Architecture In Short and point to point Oracle
  • shutdown linux Linux/Unix
  • To Find Orphan OS processes. Linux/Unix
  • Formatter Explain plan Output 1 Oracle
  • 276434.1 Modifying the VIP or VIP Hostname of a 10g or 11g Oracle Clusterware Node Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme