PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 26, 2022

[FIXED] Why my test files from a test folder can't import the source files from a source folder?

 November 26, 2022     module, project-structure, python, python-module     No comments   

Issue

I have a problem where this is my project structure:

.
├── Resources/
├── src/
│   ├── __init__.py
│   ├── main.py
│   ├── utils/
│   │   ├── __init__.py
│   │   ├── util.py
│   │   └── otherUtil.py
│   └── calculations/
│       ├── __init__.py
│       └── financials.py
└── tests/
    ├── __init__.py
    └── test.py

My problem is that I can't reach the classes from the src/ folder from the tests, although the code in src/ can reach the Resources folder, through the first shown method.

I have tried:

  1. To append the home library path this way:

    enter image description here

    Here I used the from src import util after these lines, I even tried from .src import util.

  2. Then this way:

    enter image description here

    Here I used the from src import util after these lines, I even tried from .src import util.

  3. Than without the sys.path.append() with no use.

I have tried every combination I know, but for no use, and I don't want to install them as individual packages. Does someone have an idea, witch will solve my problem?

Clarification edit:

I don't want to put the tests in the source folder, i want to keep them separate.


Solution

You can use this code found here:

# test.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/utils/')

import utils


Answered By - oflint_
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing