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.
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.
You can right click pca_mixtures and then mark the directory as Sources Root, this should solve your problem. Good luck!
How to import python file located in same subdirectory in a pycharm project - Stack Overflow
Pycharm import does not recognize other files in the same module - Stack Overflow
PyCharm error: 'No Module' when trying to import own module (python script) - Stack Overflow
python - How to make pycharm always recognize imports in the same directory? - Stack Overflow
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
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__.pyfiles are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such asstring, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case,__init__.pycan just be an empty file, but it can also execute initialization code for the package or set the__all__variable, described later.
If your own module is in the same path, you need mark the path as Sources Root. In the project explorer, right-click on the directory that you want import. Then select Mark Directory As and select Sources Root.
So if you go to
-> Setting -> Project:My_project -> Project Structure,
Just the directory in which the source code is available and mark it as "Sources" (You can see it on the same window). The directory with source code should turn blue. Now u can import in modules residing in same directory.
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 packageHow 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
From given description, it correctly imported your external module.
- Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
- Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Please try to mark directory containing your python files as Sources Root, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
