Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Reformed the answer
Source Link
John Moutafis
  • 23.3k
  • 11
  • 74
  • 118

I believe thatIf you need to fix 2 thingsfollow the structure of this guide here:http://docs.python-guide.org/en/latest/writing/structure/#test-suite (highly recommend reading it all, it is very helpful) you will see this:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules.print_module import do_stuff

To give the individual tests import context, create a tests/context.py file:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import sample

Then, within the individual test modules, import the module like so:

from .context import sample

This will always work as expected, regardless of installation method.

Translated in your case this means:

root_folder
├── job
│ ├── context.py <- create this file
│ └── the_script.py
└── modules
 ├── __init__.py
 └── print_module.py

In the context.py file write the lines shown above, but import modules instead of import samples

Finally in your the_script.py: from .context import module and you will be set to go!

Good luck :)

I believe that you need to fix 2 things:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules.print_module import do_stuff

Good luck :)

If you follow the structure of this guide here:http://docs.python-guide.org/en/latest/writing/structure/#test-suite (highly recommend reading it all, it is very helpful) you will see this:

To give the individual tests import context, create a tests/context.py file:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import sample

Then, within the individual test modules, import the module like so:

from .context import sample

This will always work as expected, regardless of installation method.

Translated in your case this means:

root_folder
├── job
│ ├── context.py <- create this file
│ └── the_script.py
└── modules
 ├── __init__.py
 └── print_module.py

In the context.py file write the lines shown above, but import modules instead of import samples

Finally in your the_script.py: from .context import module and you will be set to go!

Good luck :)

Edited due to comment
Source Link
John Moutafis
  • 23.3k
  • 11
  • 74
  • 118

I believe that you need to fix 2 things:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules.print_module import print_moduledo_stuff

Good luck :)

I believe that you need to fix 2 things:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules import print_module

Good luck :)

I believe that you need to fix 2 things:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules.print_module import do_stuff

Good luck :)

Source Link
John Moutafis
  • 23.3k
  • 11
  • 74
  • 118

I believe that you need to fix 2 things:

  1. Add a __init__.py file in the job folder so as to be recognized as a module by itself (which enables it's ability to import other modules in it)
  2. Do not name your "root" folder '.' but give it a name (ex root_folder). Then call the module as: from root_folder.modules import print_module

Good luck :)

lang-py

AltStyle によって変換されたページ (->オリジナル) /