How to import a module from outside the current folder? - python

I'm trying to import a module from outside its directory in a.py.
The directory looks something like this:
.project
├── folder_1
│ └── a.py
|
├── folder_2
│ ├── __init__.py
│ └── b.py
My code in a.py
#contents of a.py
from ..folder_2 import b.py
But from this, I'm getting this error
ImportError: attempted relative import with no known parent package
I've been searching for a solution for this problem for quite a while but I haven't been able to find anything that helps.

With the following structure:
├── project
├── __init__.py
├── folder_1
│ ├── __init__.py
│ └── a.py
├── folder_2
│ ├── __init__.py
│ └── b.py
└── main.py
your a.py will not complain when executing main.py:
from project.folder_1 import a
Since project is a package for main.py and folder_1 and folder_2 are subpackages of it, you can use
Intra-package References.
If you want to directly execute a.py, you can simply do the following:
import sys
sys.path.append('..')
from folder_2 import b

Related

pytest no module named common

I'm trying to get started with python and pytest, I have the following project structure :
.
├── my_module
│ ├── __init__.py
│ ├── common
│ │ ├── __init__.py
│ │ └── utils.py
│ └── my_script.py
└── test
├── __init__.py
└── test_my_script.py
When I run tests (using pytest), I get an error:
no module named 'common'.
I have also the following all configs files:
tox.ini
setup.py
setup.cfg
pyproject.toml
someone know what I missed?
EDIT
here is how I import utils from test_my_script.py :
from common.util import func1,func2
common.util module is not accessible from your test execution because it is located in my_module package.
You should use import starting from working directory which is your project root.
Consider using following import instead:
from my_module.common.util import func1, func2

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

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:
To append the home library path this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Then this way:
Here I used the from src import util after these lines, I even tried from .src import util.
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.
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

Python importing module from another directory

I want to import the module from another directory scripts/driver/scheduler.py to run.py and execute it, but every time I get the following error msg
ModuleNotFoundError: No module named 'scripts'
I added empty init.py files, but it didn't solve the issue
Here are a tree and the code:
.
├── __init__.py
├── pythonmodules
│ ├── module
│ │ └── run.py
│ └── setup
│ └── smthelse
└── scripts
├── driver
│ ├── __init__.py
│ └── scheduler.py
└── resources
└── smthelse
run.py
import argparse
import os.path as op
from scripts.driver.scheduler import scheduler
some lines of code
s = scheduler()
scheduler.py
import re
import sys
class scheduler():
some code
if __name__ == '__main__':
s = scheduler()
Could somebody explain why it doesn't work?
Somehow pythonpath was missing. The following export fixed it:
export PYTHONPATH=$PYTHONPATH:/home/user/myapp

pythonic style of nested path imports

I have a project with multiple subdirectories like so:
/opt/exampleProject/src
├── __init__.py
├── dir1
│   ├── __init__.py
│ ├── file.py
│ └── file2.py
└── dir2
├── __init__.py
├── file3.py
└── file4.py
My main.py file lives here
/usr/bin/main.py
I wanted to know the cleanest way to import exampleProject to be used by main. The fileX.py files also import each other and there are a lot more then shown here. What I would like to be able to do is add this to my $PYTHONPATH so that the main.py can just import them. Is there anyway to do this ?
I have been thinking of adding them all individually with.
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
But I was hoping there was a nice way to do this.
Messing with sys.path is strongly discouraged. Instead create a package for /opt/exampleProject by creating a setup.py.
/opt/exampleProject
├── setup.py
└── src
├── __init__.py
├── dir1
│ ├── __init__.py
│ ├── file.py
│ └── file2.py
└── dir2
├── __init__.py
├── file3.py
└── file4.py
afterwards install it using
pip install -e /opt/exampleProject
After doing that you can simply do
import example_project
in any other python script or package.

How should I import another python file within a module

I am building a relatively simple python module with 2 source python files.
The structure is like so:
├── MyModule
│   ├── MyModule
│   │   ├── __init__.py
│   │   ├── file1.py
│   │   ├── file2.py
│   ├── requirements.txt
│   ├── setup.py
inside of __init__.py I have
from .file1 import *
such that when imported I can simply type mymodule.myFunction()
and inside of file1.py I have
import file2
I then use pip install -e . to install the module.
However when I try to import it I get the following error:
----> 1 import file2
2 import matplotlib.pyplot as plt
3 import numpy as np
4 import scipy.signal
ImportError: No module named 'file2'
What is the accepted way I am supposed to go about doing this?
It seems like the problem was that when the file is imported into the init.py file it sees the current python environment where it is being imported and therefore cannot see file2.py. However if I instead inside of init type
from .file2 import *
from .file1 import *
and inside of file1 type
import MyModule
then I can use functions defined inside file2 inside file1 like so
MyModule.FunctionFromfile2(...)
Another way of doing this would be to initialise a sub-package as discussed here https://docs.python.org/2/distutils/examples.html .
I ended up using it as a subpackage as that was more sensible for my particular case the directory structure is now like so:
├── MyModule
│ ├── MyModule
│ │ ├── __init__.py
│ │ ├── file1.py
│ │ ├──MySubModule
│ │ │ ├── __init__.py
│ │ ├── file2.py
│ ├── requirements.txt
│ ├── setup.py
Inside of the __init__.py in MyModule I import file1 as from .file1 import * and inside of the __init__.py inside of MySubModule I import file2 as from .file2 import *.
Inside of file1 I then use MySubModule like so:
import MyModule.MySubModule
MyModule.MySubModule.FunctionFromfile2(...)
This has the benefit of separating the namespaces of the functions/objects from file1 and file2 when imported as a module. As the user sees MyModule.functionsFromfile1 and MyModule.MySubModule.functionsFromfile2.

Categories

Resources