0

enter image description here

I'm working with a Jupyter notebook. I'm trying to import a file called main.py which contains 2 lines:

import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # This is your Project Root

My code:

import os
import main.py

I'm getting the error in the screenshot. I'm using a conda virtualenv which I've activated as you can see in the screenshot. You can see in the screenshot that when I run the system path

import sys
sys.path

['',
'E:\\miniconda2\\envs\\jupyter1\\python36.zip',
'E:\\miniconda2\\envs\\jupyter1\\DLLs',
'E:\\miniconda2\\envs\\jupyter1\\lib',
 'E:\\miniconda2\\envs\\jupyter1',
 'E:\\miniconda2\\envs\\jupyter1\\lib\\site-packages',
 'E:\\miniconda2\\envs\\jupyter1\\lib\\site-packages\\IPython\\extensions',

is the path of the virtualenv python interpreter rather than my local directory. How can I fix this?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • https://github.com/jupyterlab/jupyterlab/issues/2980 – user1592380 Jun 05 '18 at 15:58
  • https://stackoverflow.com/questions/35254852/how-to-change-the-jupyter-start-up-folder – user1592380 Jun 05 '18 at 16:00
  • Your file is called a package in Python. To import a package you should use `import main`, not `import main.py`. Otherwise, it is not very obvious what you are trying to accomplish - for example, where is your notebook - is it in notebooks directory? If so, you could just use `import os; os.chdir('..')` to set the current working directory to top-level dir in your project. – krassowski Jun 06 '18 at 21:19
  • Thanks. I tried various versions of importing main and main.py. I want to add the project root directory to the python path . For some reason its not included in the sys.path. – user1592380 Jun 07 '18 at 00:50

0 Answers0