How to import python file located in same subdirectory in a pycharm project

PythonPycharmImporterror

Python Problem Overview


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, util/util.py. I have another file in util called run_tests.py.

In run_tests.py, I have the following import structure,

from util.util import my_functions, etc.

This yields an input error, from util.util import load_dataset,proportionate_sample ImportError: No module named 'util.util'; 'util' is not a package


However, in the exact same project, in another directory (same level as util) called data, I have a file data/data_prep.py, which also imports functions from util/util.py using a similar import statement...and it runs without any problems.


Obviously, I am doing this in the course of doing a homework, so please understand: this is ancillary to the scope of the homework.


The problem goes away when I move the file to another directory. So I guess this question is How do I import a python file located in the same directory in a pycharm project? Because pycharm raises an error if I just do import util and prompts me to use the full name from the root.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

Right-click on the folder which you want to be marked as the source > Mark Directory as > Source root.

Solution 4 - Python

Note: May be a bit unrelated.

I was facing the same issue but I was unable to import a module in the same directory (rather than subdirectory as asked by OP) when running a jupyter notebook (here the directory didn't have _init_.py). Strangely, I had setup python path and interpreter location and everything. None of the other answers helped but changing the directory in python did.

import os
os.chdir(/path/to/your/directory/)

I'm using PyCharm 2017.3 on Ubuntu 16.04

Solution 5 - Python

I had the same issue with pycharm, but the actual mistake was that the file I was trying to import didn't have a .py extension, even though I was able to run it as a standalone script. Look in the explorer window and make sure it has a .py extension. If not, right click on the file in the explorer window, pick refactor, and then rename it with a .py extension.

Solution 6 - Python

In my case, it worked only when I omit the extension. Example:

import filename

Solution 7 - Python

In Pycharm go to "Run - Configuration" and uncheck 'Add Content root to Pythonpath' and 'Add source roots to Pythonpath', then use

from filename import functionname

Solution 8 - Python

For me the issue was, the source directory was marked correctly, but my file to import was named starting with numeric value. Resolved by renaming it.

import 01_MyModuleToImport

to

import MyModuleToImport

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - PythonimanzabetView Answer on Stackoverflow
Solution 2 - PythondanideeView Answer on Stackoverflow
Solution 3 - PythonSrikar DurgiView Answer on Stackoverflow
Solution 4 - PythonMr. Unnormalized PosteriorView Answer on Stackoverflow
Solution 5 - Pythonuser5099519View Answer on Stackoverflow
Solution 6 - PythonlfvvView Answer on Stackoverflow
Solution 7 - PythonandrupoView Answer on Stackoverflow
Solution 8 - PythonNiharGhtView Answer on Stackoverflow