It is better if you replace that with:

from pca_mixtures.constants import *

The issue is because of how PyCharm sets PYTHONPATH to the root of your project.

While you can add directories as Sources, I would not recommended. That will make your project fragile.

Answer from Juan Leni on Stack Overflow
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000424900-Very-frustrated-PyCharm-says-local-import-is-unresolved-reference
Very frustrated--PyCharm says local import is unresolved reference – IDEs Support (IntelliJ Platform) | JetBrains
August 13, 2018 - So you need to change the structure of your app in line with a single heirarchy / tree and only import from same level of tree or higher. You leaves cannot import from other leaves of the tree.
Discussions

How to import python file located in same subdirectory in a pycharm project - Stack Overflow
I have an input error in pycharm when debugging and running. My project structure is rooted properly, etc./HW3/. so that HW3 is the root directory. I have a subfolder in HW3, util, and a file, u... More on stackoverflow.com
🌐 stackoverflow.com
Pycharm import does not recognize other files in the same module - Stack Overflow
Say, my project structure is as following: project_folder: __init__.py another_folder: __init__.py main_file.py other_file.py And main_file.py contains next code: import other_fil... More on stackoverflow.com
🌐 stackoverflow.com
PyCharm error: 'No Module' when trying to import own module (python script) - Stack Overflow
This is because python only looks in the current directory and sys.path, and so wont find my_mod.py unless it's in the same directory · Or you can look here for an answer telling you how to import from other directories. As to your other questions, I do not know as I do not use PyCharm. More on stackoverflow.com
🌐 stackoverflow.com
python - How to make pycharm always recognize imports in the same directory? - Stack Overflow
I have a file a.py in a directory somewhere inside the project, and in the same directory I have b.py. Inside a.py I want to be able to do: import b This works in Python, but Pycharm doesn't recog... More on stackoverflow.com
🌐 stackoverflow.com
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-42748 › PyCharm-doesnt-see-the-modules-to-import-in-the-same-directory
PyCharm doesn't see the modules to import in the same ...
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-43105 › PyCharm-EAP-Importing-another-Python-file-in-same-directory-shows-up-as-error-but-runs-okay.
PyCharm EAP: Importing another Python file in same ...
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-60666 › PyCharm-cant-find-imports-for-nested-package-directories
PyCharm can't find imports for nested package directories : PY-60666
You only need to mark the parent directory of the package that you want to import as Sources Root. So, for instance, if you want to import from src.package import foo and src is inside a dir directory, then you need to mark the dir as Sources Root for the import to be resolved.
Top answer
1 of 8
116

Recommended Way:

Make sure to set the working folder as Sources.

You can do it in Pycharm -> Preferences -> Project: XYZ -> Project Structure

Select your working folder and mark it as Sources. Then Pycharm recognize the working folder as a Source folder for the project and you will be able to simply add other files within that folder by using

import filename.py

or

from filename.py import mudule1

=================

Not recommended way:

In Pycharmyou can simply add . before the .py file which you are going to import it from the same folder. In your case it will be

from .util import my_functions

Resource

There is a good reference also for more information with example how to implement Package Relative Imports. I would highly recommend to check this page.

Package Relative Imports

2 of 8
23

If you don't have an __init__.py create one and add this line

from util.util import my_function

then you can easily import the module in your scripts the __init__.py tells python that it should treat that folder as a python package, it can also be used to import/load modules too.

in most cases the __init__.py is empty.

Quoting the docs:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 115000782850-How-to-set-up-working-directory-in-PyCharm-and-package-import
How to set up working directory in PyCharm and package import? – IDEs Support (IntelliJ Platform) | JetBrains
December 18, 2017 - 1) I want to import the file lr_utils.py as a package, it's in my current directory. PyCharm refuse to read it. So, I specified the absolute path, It still doesn't work. Why? os.chdir("/Users/Branden/Desktop/Deep Learning") from lr_utils import load_dataset · If I ran the same code in another editor like Sublime Text, Spyder, or iPython, it works. Why PyCharm does not recognize the module?
Find elsewhere
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360008168000-PyCharm-doesn-t-recognize-installed-modules
PyCharm doesn't recognize installed modules – IDEs Support (IntelliJ Platform) | JetBrains
3. copy the module from the location(location shown in command prompt) 4.paste the module in working directory location(paste in site-package inside lib) ... I have a similar problem. I using custom modules that are inside a custom dir. This dir is added to PYTHONPATH env variable. Now, PyCharm does not recognize those modules when I try to import to any file in my project. Guess what. The same project/file I open in Visual Studio Code and everything works fine.
🌐
Michelwolfsa
michelwolfsa.ch › wp-content › themes › mantra › uploads › renault-workforce-2412 › pycharm-does-not-recognize-import-from-same-directory.html
Pycharm does not recognize import from same directory
You appear to have set up your Python path so that the webapp directory is on the path, which is why packages under it Jan 25, 2008 · Everything that people seem to list as advantages of PyCharm can be included with a few clicks in VS Code - remote interpreter and debuger - running env and ...
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360001787400-PyCharm-not-resolving-import
PyCharm not resolving import – IDEs Support (IntelliJ Platform) | JetBrains
November 26, 2018 - Pycharm is consistently not resolving the "baselines" import (but it runs fine). I'm really not sure why! ... I collect type information, in case this also collects info about imports. ... Looking at the screenshots, marking baselines directory as Sources should do the trick or using the whole path (bacelines.baselines.).
🌐
YouTube
youtube.com › meri (engel) kasprak - programming videos
Fixing the problem where PyCharm is not recognizing your custom import file - YouTube
This happens when you have your project organized in a folder structure, and you are not working at the top level. It is an easy fix, if you know what to fix.
Published: July 28, 2020
Views: 62K
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360001219839-from-XYZ-import-pyCharm-not-seeing-it-python-2-7-
from XYZ import * — pyCharm not seeing it (python 2.7)
October 15, 2018 - When marking some parent folder as Sources, PyCharm will not look for your module in all subfolders, it will look for the module in this specific directory. If you need to import some module from subdirectory, then you can either mark it as Sources or use already known to you `from path.to.module.XYZ import *`.
🌐
Reddit
reddit.com › r/learnpython › importing other scripts in the same folder only works if i run the script through pycharm config
r/learnpython on Reddit: Importing other scripts in the same folder only works if I run the script through PyCharm config
May 9, 2021 -
project-directory
-- python
---- a.py
---- b.py

I have a file structure like this in my PyCharm project directory, and at the top of a.py:

import python.b

This all works well when running through the PyCharm configuration, but when I try to run the same script in the command line, it throws the error:

File "python/a.py", line 15, in <module>
    import python.b
ModuleNotFoundError: No module named 'python' 

Why does that think I'm importing from a module?

I searched online and also tried to replace the way I imported with this:

from python import b

It throws the same error

and I also tried this:

from . import b

and I got a new Error:

File "python/a.py", line 15, in <module>
    from . import b
ImportError: attempted relative import with no known parent package

How can I fix this?

For context, I want to run the script the following way:

python -m trace -t --ignore-dir C:\ProgramData\Anaconda3\envs\project_name python/a.py
🌐
Stack Overflow
stackoverflow.com › questions › 61252390 › pycharm-import-file-in-the-same-directory
python - PyCharm import file in the same directory - Stack Overflow
How can i fix this faulty behaviour of PyCharm ? ... @MachineLearner i have installed every neccessary dependency, like tensorflow and foolbox with "pip install" and there are no error messages about them. The error messages are only when i import another file in the same directory. Not sure how to configure the interpreter any more, any tips ?
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-43105 › PyCharm-EAP-Importing-another-Python-file-in-same-directory-shows-up-as-error-but-runs-okay
PyCharm EAP: Importing another Python file in same directory ...
December 19, 2022 - Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › articles › 360010202240-PyCharm-can-t-install-import-a-package-library-module
PyCharm can't install/import a package/library/module – IDEs Support (IntelliJ Platform) | JetBrains
February 5, 2020 - Note: Having installed something from the system terminal/cmd prompt doesn't always mean it will be available in PyCharm. Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment.