简体   繁体   中英

Importing own modules in Anaconda Python

I am currently using conda 4.9.1 with the spyder-ide 4.1.4. Importing own written modules in VSCode worked for me in a straightforward fashion, but the spyder-ide is using an own python interpreter, hence I am a bit stucked.

Anaconda is installed on C. My folder tree on drive D:\ (Python Anaconda is installed on C:) looks as follows:

D:Top_package
|   
|   
+---level_one_a
|   |   my_lib.py
|   |   __init__.py
|   |   
|   \---level_two
|           hello_world.py
|           __init__.py
|           
\---level_one_b
        my_lib_b.py
        __init__.py

I am trying to execute hello_world.py from "D:\Top_package\level_one_a\level_two", whereas hello_world.py contains following code

import sys
import os

sys.path.append('D:\Top_package\level_one_a')

from Top_package.level_one_a import my_lib

print(add(5,3))

As you can assume my_lib.py under "D:\Top_package\level_one_a" is defined as

def add(a,b):
    return a+b

I am getting the error and tried everything. Importing modules under Visaul Studio works fine. Anaconda yields me problems though.

runfile('D:/Top_package/level_one_a/level_two/hello_world.py', wdir='D:/Top_package/level_one_a/level_two')
Reloaded modules: ipykernel, ipykernel._version, ipykernel.connect, ipykernel.kernelapp, zmq.eventloop, zmq.eventloop.ioloop, tornado.platform, tornado.platform.asyncio, tornado.gen, zmq.eventloop.zmqstream, ipykernel.iostream, jupyter_client.session, jupyter_client.jsonutil, jupyter_client.adapter, ipykernel.heartbeat, ipykernel.ipkernel, IPython.utils.tokenutil, ipykernel.comm, ipykernel.comm.manager, ipykernel.comm.comm, ipykernel.kernelbase, tornado.queues, tornado.locks, ipykernel.jsonutil, ipykernel.zmqshell, IPython.core.payloadpage, ipykernel.displayhook, ipykernel.datapub, ipykernel.serialize, ipykernel.pickleutil, ipykernel.codeutil, ipykernel.eventloops, ipykernel.parentpoller, pyparsing, cycler, kiwisolver, win32api, win32security, ntsecuritycon, IPython.core.completerlib, storemagic, autoreload, PIL, PIL._version, PIL.Image, PIL.ImageMode, PIL.TiffTags, PIL._binary, PIL._util, PIL._imaging, cffi, cffi.api, cffi.lock, cffi.error, cffi.model, PIL.PngImagePlugin, PIL.ImageChops, PIL.ImageFile, PIL.ImagePalette, PIL.GimpGradientFile, PIL.GimpPaletteFile, PIL.ImageColor, PIL.PaletteFile, PIL.ImageSequence, dateutil.rrule, mpl_toolkits.mplot3d, mpl_toolkits.mplot3d.axes3d, mpl_toolkits.mplot3d.art3d, mpl_toolkits.mplot3d.proj3d, mpl_toolkits.mplot3d.axis3d, ipykernel.pylab, ipykernel.pylab.backend_inline, ipykernel.pylab.config, spyder, spyder.pil_patch
Traceback (most recent call last):

  File "D:\Top_package\level_one_a\level_two\hello_world.py", line 6, in <module>
    from Top_package.level_one_a import my_lib

ModuleNotFoundError: No module named 'Top_package'

The interpreter I am using the the default spyder one and I would like to stay with it. Switching to a properly installed python version under PYTHONPATHmanager - which is empty as of now - would render my shift from VSstudio to Anaconda useless. What am I doing wrong?

The sys variables are as follows:

print(sys.path)
['', 'C:\\Users\\admin\\AppData\\Local\\Programs\\Spyder\\Python\\python37.zip', 'C:\\Users\\admin\\AppData\\Local\\Programs\\Spyder\\Python', 'C:\\Users\\admin\\AppData\\Local\\Programs\\Spyder\\pkgs', 'C:\\Users\\admin\\AppData\\Local\\Programs\\Spyder\\pkgs\\IPython\\extensions', 'C:\\Users\\admin\\.ipython', 'D:\\Top_package\\level_one_a']

I've found the solution finally. All you have to do is checking for spyders packages path (in my case 'C:\Users\admin\AppData\Local\Programs\Spyder\pkgs') and add your modules there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM