0

I tried writing some commenly used function in a seperate file and import the same into mainApp file, but not able to use import.

I did find many questions regarding the this same question but, the solution was to keep the files in the same folder

enter image description here

I tried without .py as well, but the same error:

enter image description here Can you please help me how can i fix this issue ?

pppery
  • 3,731
  • 22
  • 33
  • 46
user1743673
  • 99
  • 2
  • 3
  • 11
  • Please [edit] your answer to say *what* error you are receiving. PyCharm automatically sets some module search paths, and the exact issue max depend on them. – MisterMiyagi Jun 16 '20 at 14:01

4 Answers4

0

No '.py'. Just import seperate

Sơn Ninh
  • 331
  • 1
  • 10
  • you should set up the root directory first. https://www.jetbrains.com/help/pycharm/configuring-content-roots.html – Sơn Ninh Jun 16 '20 at 13:46
0

Try using this in mainApp.py:

from seperate import *
a()

where seperate.py looks like this:

def a():
    print('hi')

Well, sorry, those two files need to be in the same folder. This is not a solution to your problem.

ZackDev
  • 3
  • 4
0

The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:

from .some_module import some_class
from ..some_package import some_function
from . import some_class

Read more about Absolute vs Relative Imports in Python

In your case it should be:

from .seperate import a

Also check this question: Importing from a relative path in Python

Aleksey Razbakov
  • 624
  • 8
  • 29
-1

add your project directory into your path variable so that python know from where you want to import file