Python cannot import py files in the same folder

python import class from file in another directory
importerror: attempted relative import with no known parent package
python cannot import module
python cannot import name
python import module from directory
importerror: cannot import name python3
modulenotfounderror: no module named
python relative import
  • VSCode Version: 1.41.1
  • OS Version:Ubuntu 18.04

Steps to Reproduce:

# tree:
.
├── demo1
│   ├── __init__.py
│   └── test.py
├── __init__.py
├── auto.py
# auto.py
def func():
    print("1")
# test.py
from auto import func

func()

Use examples to solve problems that arise in a project Run the test.py file, and I get "ModuleNotFoundError: No module named 'func'" I used 'CTRL '+ left mouse button in test.py to jump to func The same code can be run in pycharm

If you run test.py directly then you need to add the parent folder to PYTHONPATH. Try:

import sys
sys.path.append("..\<parent_folder>")
from auto import func

Otherwise, if you merely want to import test.py in another .py file, you can use relative import of python

from . import auto #another dot '.' to go up two packages
auto.func()

Reference

Traps for the Unwary in Python's Import System — Nick Coghlan's , However, if we remove the offending __init__.py file, Python 3.3+ will standard library, as the apparent socket module in the current directory gets in the way: in <module> from socket import socket ImportError: cannot import name socket� I had a big folder of Python practice projects open in VS Code and I was having big problems with finding things in the same directory. My problem was that, in the terminal, I hadn't changed directories to the sub-directory I had my current project in.

Add this in test.py, before import:

import sys
sys.path.insert(0, "/path/to/project/root/directory")

For me it's not a good file organization. A better practice might be as below:

Let your project file tree be like:

.
├── __init__.py
├── lib
│   ├── auto.py
│   └── __init__.py
└── test.py

And write test.py like:

from lib.auto import func

func()

Python can't find local files/modules, What puzzles me is that config.py is located in the same folder as also running Python 3.7 and inside a virtualenv, and the importing (and the� the best way to import .py files is by way of __init__.py. the simplest thing to do, is to create an empty file named __init__.py in the same directory that your.py file is located. this post by Mike Grouchy is a great explanation of __init__.py and its use for making, importing, and setting up python packages.

Simple one-line solution

from ... import auto

and call the function by using auto.func().

I can't import .py file in same directory : learnpython, I have a python file establishing a class, and I would like to use this class in another file. This two python file is in same file folder, however … A Quick Recap on Imports. You need to have a good understanding of Python modules and packages to know how imports work. A Python module is a file that has a .py extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an __init__.py file).

The Definitive Guide to Python import Statements, We also have a script called a_script.py in a directory called scripts . We want to be able to import the code in a_module.py to use in a_script.py . So, we want to be able to So, Python is looking for a file a_module.py , and not finding it. You will see the same effect at the interactive Python console, or in IPython: >>> import� Now, we can import the sum module using the “import sum” inside the other file. Let’s import in the app.py file, which is in the same directory as the sum.py file. # app.py import sum. So, the import syntax is the following. Python import syntax import modulename. In our example, modulename = sum. Now, we can use the add() function of the

Where does Python look for modules? — Functional MRI methods, Although, not every Python file is designed to be imported as a module. For example a for.py module can be created, but cannot be imported because "for" is in the same directory as the .py file for Python versions up to 3.2, and in directory� Inside the first directory, there is file plane.py with the class Plane. Inside the directory, there is an __init__.py file. This file is essential in Python 2 and older versions of Python 3. It’s not necessary for Python 3.3 and newer. They offer Implicit Namespace Packages. This means that you are not required to create __init__.py files.

Python Modules: Creating, Importing, and Sharing, For example, you can write the following to a file test_argv.py: You can add any path you want, not necessarily the current directory. that once you use relative imports, you can't simply run your files using python file_2.py� import SomeObject since it is in the same folder. For TestCase.py, do. from ..myapp import SomeObject However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:

Comments
  • working directory has to be the root of the file tree, i do not know how to set it in visual studio code (imported modules are imported relative to the working directory when you just run a file)
  • Please note, that importing from outside the package is almost always a bad idea. It involves messing with relative imports, which is, well... messy. Always try to structurize your project to avoid such problems. See python.org/dev/peps/pep-0008/#imports
  • Thank you. I just transferred from pycharm to vscode. Previous projects could run without introducing sys, but this one had problems