2

I have two projects. In first one, I can import my module importme.py like:

import importme

And now I can use my function hello() in importme module without any problem. In second one, I recieve:

ImportError: No module named 'importme'

But I can import it via:

from . import importme

Why I cant import my module the same way in both projects? Should I configure some paths ?

EDIT1:

Directory structure of first project:

testproject/
├── importme.py
└── start.py

Directory structure of second project:

spiders/                                                                                                                                                                                                                                                                                                                     
├── spider.py                                                                                                                                                                                                                                                                                                           
├── download_page.py                                                                                                                                                                                                                                                                                                         
├── importme.py                                                                                                                                                                                                                                                                                                              
└── __init__.py  

file init.py is empty.

dorinand
  • 1,397
  • 1
  • 24
  • 49

1 Answers1

0

My favorite method of dealing with PYTHONPATH is installing package in edit mode in virtual environment.

  1. Creating virtual environment

    # create
    $ python -m venv ~/virtualevns/myenv
    # then activate it
    $ source ~/virtualenvs/myenv/bin/activate
    # you can check whether it got activated
    $ which python
    home/user/virtualenvs/myenv/bin/python
    
  2. Writing setup.py for your project. For this refer to the official distributing packages tutorial.

  3. Installing package in editable format.

    If you install package with -e flag pip will install it in editable format meaning all changes you have in code will be present in your environment's package:

    $ cd mypkg
    $ pip install -e .
    
  4. Finally you need to set your virtual-environment to be used in whatever IDE your editor you are using.

This is a great workflow because it's clean and reliable - you are using exactly what you'd be using in production/finished package environment.

Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
  • I am using venv, but the problem is, that in first example, I can import module but in second one I cant. So my question is, why, where is the difference and how to solve it. – dorinand May 02 '18 at 10:15