PyCharm unresolved reference when importing from the same folder - python

When I just created a new project I had two py files - main.py and functions.py - in the project folder "my project". In main.py I imported functions.py and it worked fine. But then I created some folders in "my project" folder and put main.py and functions.py in one of them. They are still in the same folder, but in main.py it says that functions.py is an unresolved reference.
P.S. Using PyCharm on macos

Simple answer: you should make a package in your directory tree and PyCharm will pick it up easily.
project_root
├── my_project
│   ├── __init__.py
│   ├── main.py
│   ├── functions.py
Open project_root in PyCharm and these imports should work correctly:
from my_project import functions
or
import my_project.functions
# you can also do 'import my_project.functions as functions'
my_project.functions.do_something('argument')

Related

Python Library - Module Not Found

I'm making a python library and i want to be able to run the code i'm developing, in another folder i have a python file, but I get the error: ModuleNotFoundError: No module named
this is my folder structure
.
└── project
└── library_directory
├── __init__.py
└── main.py
└── examples_directory
├── __init__.py
└── code_directory
├── __init__.py
└── test.py
init.py from library_directory
from library_directory.main import Class
test.py file
from library_directory import Class
when I run test.py file it says: ModuleNotFoundError: No module named 'fpdf_table'
if i put test.py file at project level this configuration of init and test works, but i want to run the test.py in the code_directory because i will have a lot of files and don't want 15+ single files at project level
.
└── project
└── library_directory
├── __init__.py
└── main.py
└── examples_directory
├── __init__.py
└── code_directory
├── __init__.py
└── test.py
i already tried absolute and relative imports but they don't work
Im not sure if this is the correct solution, but you can try this:
In your test module:
import sys
sys.path.insert(0, '/Your_project_root_path'
Now can access to the packages in the root directory.
I took this solution from here.
Your library_directory folder is a Python package. Within the package, each .py file is a module.
In your library_directory/init.py file, insert the line from .main import Class. Now you have a package called "library_directory", which has a module called "Main", and a class called "Class".
Now, in your environment variables, create a user variable called PYTHONPATH and add to this, the path to your project directory.
When you import in your project file, you should import using the structure: from package.module import class, or in your case: from library_directory.main import Class. The python interpreter will be able to find the package due to being in your PYTHONPATH and the init file directory means Python will recognise it as a package.
(You may wish to rename "library_directory" to be a bit more project specific)

attempted relative import with no known parent package errors when trying to import from local folder?

I have this folder structure in my Python project:
MY-PROJECT
.vscode
myfolder1
├── .venv
├── myconfig.yaml
├── myproject.toml
├── poetry.lock
└── myfolder2
├── __init__.py
├── __main__.py
├── config.py
└── mycode.py
From mycode.py I am trying to do a from . import __version__ and from .config import configuration. My understanding is that it should work as mycode.py is in the same folder as __init__.py and config.py, but I get the following error:
attempted relative import with no known parent package
File "Volumes/myvolume/myfiles/MY-PROJECT/myfolder1/myfolder2/mycode.py", line 12, in <module>
from . import __version__
Python version is 3.9.7 (using poetry).
Any ideas on what might be wrong?
I have found the answer :)
Instead of ., if you manually add the folder name the code works as expected.
from myfolder2 import __version__

How do I structure my python project such that my test files can import the packages in the root folder?

I would like to integrate pytest into my workflow. I made a following folder structure:
myproject
├── venv
└── src
├── __init__.py
├── foo
│ ├── __init__.py
│ └── bar.py
└── tests
├── __init__.py
└── test_bar.py
I would like to be able to import the namespace from the foo package so that I can write test scripts in the tests folder. Whenever I try to run pytest, or pytest --import-mode append I always get the following error:
ModuleNotFoundError: No module named 'foo'
I found this similar question here but adding the __init__.py files to the tests and the src folder does not solve the issue.
Does this have to do with the PYTHONPATH system variable? This folder structure works perfectly if I run the __main__.py from the src folder, but fails when I want to use pytest. Is there a way to do this without having to mess with PYTHONPATH or are there automated ways to edit the system variable?

Project module not found outside virtualenv

My project has basically an architecture like this:
src
├── __init__.py
├── main.py
└── core
├── __init__.py
├── module1.py
└── module2.py
All __init__.py files are empty, in main.py I have a from src.core.module1 import stuff and I am running main.py from the src folder.
When I'm running it from my project virtual environment everything works fine, but outside of the virtual environment I have an ImportError: no module named src.core.module1. I cannot understand why, because this module is in the project, not related to the Python environment packages...
(Windows / Python 2.7.14)
Try from core.module1 import stuff. You should not import src since you are in that location with your main.py already.

Airflow Relative Importing Outside /dag Directory

I haven't been able to move common code outside of the dag directory that airflow uses. I've looked in the airflow source and found imp.load_source.
Is it possible to use imp.load_source to load modules that exist outside of the dag directory? In the example below this would be importing either foo or bar from the common directory.
── airflow_home
|──── dags
│ ├── dag_1.py
│ └── dag_2.py
├── common
├── foo.py
└── bar.py
Just add __init__.py files in all 3 folders. it should work.
Infact every folder in my folder structure having __init__.py. I could run the code and see output.
Example folder structure could be as:
── airflow_home
├── __init__.py
|──── dags
│ ├── __init__.py
│ ├── dag_1.py
│ └── dag_2.py
├── common
├── __init__.py
├── foo.py
└── bar.py
and dag_1.py code can be as:
from stackoverflow.src.questions.airflow_home.common.bar import bar_test
def main():
bar_test()
main()
I'm running this piece of code from my pycharm.
Your airflow_home's folder path in my pycharm is stackoverflow/src/questions/airflow_home/
And bar.py code is
def bar_test():
print "bar hara"
Add your airflow home path to PYTHONPATH
export AIRFLOW_HOME=/usr/local/airflow
export PYTHONPATH="${PYTHONPATH}:${AIRFLOW_HOME}"
Dockerfile
ENV AIRFLOW_HOME=/usr/local/airflow
ENV PYTHONPATH "${PYTHONPATH}:${AIRFLOW_HOME}"
Other way instead of adding __init__.py file is to add the following include at the top of dag script:
import sys
import os
sys.path.insert(0,os.path.abspath(os.path.dirname(__file__)))
This works for me, I'm running airflow on docker by the way:
import sys
import os
sys.path.append(os.path.abspath(os.environ["AIRFLOW_HOME"]))
After running into the same problem, this fixed it for me:
import sys, os
sys.path.insert(0,os.path.abspath(os.path.join(os.path.dirname(file),os.path.pardir)))
from common.foo import *

Categories