How can I import a module from a parent directory? - python-3.x

I have the following directory structure for my Python project:
lib
__init__.py
tasks.py
api(directory)
__init__.py
app.py
Now, I want to import the tasks module into app.py. If I just type
import tasks
it magically works in Pycharm, but when executed via bash I get an error message saying "No module named tasks". I also tried
from .. import tasks
which gives me
ValueError: attempted relative import beyond top-level package
What am I doing wrong? Why does import tasks work when executed in Pycharm?

Related

ModuleNotFoundError: No module named 'module' while importing my own module VsCode MacOs Python 3.10

Structure:
|--folder/
|--a.py
|--main.py
When loading module 'main' into module 'a'
#a.py
import main
the following error occurs - ModuleNotFoundError: No module named 'main'.
PyCharm copes with this task, but VSCode does not. What's the matter?
Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.
import sys
sys.path.append('/path_to_main_module_directory/')
import main
print("This import is for the main module")
I hope this helps!
[1]: Importing files from different folder

Relative imports in python and pycharm

I have following project structure and i'm using PyCharm.
Project/
subproject1/
main.py
modules/
1.py
__init__.py
subproject2/
main.py
modules/
subproject3/
__init__.py
file.py
I need in Project/subproject/modules/1.py a class from Project/subproject3/file.py. I tried it with from ...subproject3.file import class1, but then i get the error ValueError: attempted relative import beyond top-level package. Next i tried the import from subproject3.file import class1, and that work's, but i don't know why. My working directory in PyCharm is in Project/ but i tried to create a Python application with pyinstaller and that worked also for me and a received no error message. Why does my file 1.py knows from subproject3 and i can import it that way?
Thank you!!

Visual Studio Code Unit Tests Environment

I have the following working directory with my source and tests folders as illustrated
root_folder:
tower_shell
init
read_files
files_processing
tests
init
test_read_json.py
I want to run the unit test test_read_json.py from my folder tests which need the following imports:
import unittest
import os
import sys
from tower_shell.read_files import read_json
from pathlib import Path
The problem is that the file read_files (contained in tower_shell) folder is also importing a module from tower_shell called files_processing: \
from files_processing import process_hub_sheet, process_tower_sheet
Since my working directory is one level up, it raises an error stating that the module can not be imported.
I tried to add PYTHONPATH variable but the test discovers is not functioning then.
Thanks

Python3 test folder import problems

Trying to write tests in Python3 with the folder structure
package/
__init.py__
Foo.py
Bar.py
test/
__init.py__
test_Foo.py
Foo.py
import Bar
test_Foo.py
import Foo
Running python3 -m unittest results in No module named Foo
If I changed it to
from package import Foo
It will resolve, but in Foo.py it'll have the error No module named 'Bar'
If I change Foo.py to
from package import Bar
The test will run without error, but running python3 Foo.py will result in No module named 'package'
I've gone through various answers about this kind of problems, like this one
Running unittest with typical test directory structure
and this
Python3 import modules from folder to another folder
and none of them address this issue
your project root (and also the working directory!) need to be the parent of package.
Then every import will be in the form import package.module or from package import module.
if you run python packgae/Foo.py or python -m package.Foo it should work.
Also take a look at pypa - sample project to see the recommended Python project structure.

import package from an other package

I have a very simple project architecture:
project/
__init__.py
dira/
__init__.py
a.py
dirb/
__init__.py
b.py
All the __init__.py are empty.
a.py only contains a "hello" function:
def hello(): return "hello world"
I want b.py to use the hello function from a.py.
What I tried so far:
Following answers from this question, I tried:
import ..dira.a as aa
aa.hello
But when running b.py I get a syntax error on ..a.a
I also tried to manually add the project directory to sys.path and then run:
import dira.a as aa
aa.hello
But when running b.py I get:
ImportError: No module named 'a'
What should I do? Why the solution I found doesn't work?
Short answer:
import ..dira is invalid syntax and only from ..dira.a import hello works
Working configuration: b.py:
from ..dira.a import hello
print(hello())
Also, from docs:
"Note that 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
must always use absolute imports."
So if you do relative imports you cannot call b.py directly. Apperently, you cannot do it from project level either. (Python interpreter in project folder, call import dirb.b)
An error occurs:
ValueError: attempted relative import beyond top-level package
But calling import project.dirb.b makes proper output:
hello world
EDIT: Why import project.dirb.b can be called only outside of project module?
Let's look at the problem from interpreter's side.
You call import project.dirb.b. It runs dirb/init.py, 'current module' is project.dirb
Interpreter finds command from ..dira.a import hello
It treats double dot ..dira.a as "move one module up and go to dira and import a.hello". 'Current module' is project.dirb, so parent module is project, thus it finds parent.dira
What happens when you call import dirb.b directly? It fails on trying to go level up in module hierarchy and reports error.
Thus, relative import makes project.dirb.b importable only outside of project.

Resources