1

Professional Python newbie here. I have created a Python module called aviation with a file in there called database.py.

I have another module called core, with a file in there called calculator.py.

I want to import aviation.database.py into my calculator.py.

The basic structure is as follows:

My project
    aviation (module)
        - database.py
    core (module)
        - calculator.py
    test.py

My calculator.py file has an import such as:

from aviation import database as aviation_database

This module is not recognised and I get a red squiggly line indicating as much.

If I create another file test.py outside of aviation and core and add the above import, there are no issues in this tests.py file - the import works fine.

It appears that I need to do something so that my module can import from another module... it does allow me to import installed modules (like date), but I have no idea what I am missing.

I am using the IntelliJ IDE and my code is located in the regular C:\Users\\IdeaProjects directory.

Can someone tell me what I should do and why I am facing this problem?

pookie
  • 3,796
  • 6
  • 49
  • 105
  • I'd say you have to add 'My project' [pythonpath](http://stackoverflow.com/questions/19917492/how-to-use-pythonpath). – carlosvin Apr 03 '17 at 11:32
  • @carlosvin Hmm, I have to modify system variables just to have my IDE know where the files that were created with said IDE, are? Why is it that it works from `test.py`? I'm not trying to run my code from any random location on my machine. – pookie Apr 03 '17 at 11:40
  • you don't have to modify your system variable, you can configure your IDE to add 'My project' to python path when you run the script. I am guessing you are running 'python test.py', that means that python is adding 'My project' directory to execution python path. You can try to run 'python core/calculator.py' – carlosvin Apr 03 '17 at 11:48
  • You might want to provide more info about your IDE and which directory are you running python from – carlosvin Apr 03 '17 at 11:51
  • @carlosvin Thanks, updated. – pookie Apr 03 '17 at 11:53
  • IntelliJ has a nice feature that is mark directory as source root https://www.jetbrains.com/help/pycharm/2016.3/configuring-folders-within-a-content-root.html You have to do so with 'My project' – carlosvin Apr 03 '17 at 13:15
  • Yeah, I did find that `as source` option and I did try it, but there is no difference. Interestingly, I have found that my code does run, but IntelliJ still shows errors on imports... – pookie Apr 04 '17 at 09:18

2 Answers2

0

Well I would recommend you to create __init__ file in the modules.

The python3 doc states that :

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.

So your Directory Structure becomes

My project
    aviation (module)
        - database.py
        - __init__.py
    core (module)
        - calculator.py
        - __init__.py
    test.py

Now import all the thing in your __init__ file that you want to us from other packages. Like in aviation's __init__.py file write

import database 

Similarly, for core's __init__.py Now in calculator.py you can import them by -

from aviation import database

Let me know, if this help !

Thanks

Shivam Kotwalia
  • 1,419
  • 2
  • 15
  • 20
  • Thanks, I had tried this but I don't think for Python 3 we need to use __init__.py. I think it is just for packages (collections of modules). Anyway, it doesn't work. – pookie Apr 04 '17 at 09:16
  • Then a simple heck will work for you, append the path of your project in sys.path – Shivam Kotwalia Apr 04 '17 at 09:31
0

Its as simple as this.

For instance, you have an already written code e.g myname.py and you want to import the entire module into new code e.g newwork.py

step 1. open your code newwork.py
step 2. just as you do other import, just write:

import myname 
myname

Thats all.

Now, you have your entire nyname code inside newwork. When you run newwork, it runs nymame alongside.

I hope this help somebody?

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46