2

I am new to PyCharm and am having difficulty importing modules that I have written into the Python console. If I try to import a module that is native to Python I can import that module without difficulty but if I try to import a module that I have written I get an ImportError: No module named 'ModuleITriedToImportName'. For instance here is a simple self written module to pickle files called "filepickle":

import pickle

def saveDbase(filename, object):
    file = open(filename, 'wb')
    #pickle.dump(object, file)       # pickle to file
    #pickle.dump(object, open(filename, 'wb'))
    pickle.dump(object, file)
    file.close()                     # any file-like object will do

def loadDbase(filename):
    file = open(filename, 'rb')
    object = pickle.load(file)       # unpickle from file
    file.close()                     # recreates object in memory
    return object

If I try to "import pickle" at the PyCharm Python Console then the import works without any error. If I try to "import filepickle" I receive the error message:

ImportError: No module named 'filepickle'

The module filepickle works just fine if I run filepickle within PyCharm but I am unable to import filepickle in the Python console. If anybody knows how to get PyCharm to allow me to import modules that I have written into the PyCharm Python console I would appreciate the help.

user3798654
  • 431
  • 2
  • 9
  • 19
  • do you mean you wrote a module called filepickle and want to import it? in which case you need to save the module to a file not just write it in the console. There is no way to import code that is executing in an interactive console. – Tadhg McDonald-Jensen Mar 09 '16 at 20:24
  • What directory does `filepickle.py` live in? Is it the the same directory where you run the Python console? – John Gordon Mar 09 '16 at 20:25
  • Hello, Yes I wrote a module called filepickle and want to import it. Yes I did save it as a python file I did not just write it in the console. filepickle.py lives in a database directory that I have each module I have written for this project in. I run the Python console from within Pycharm. – user3798654 Mar 09 '16 at 20:35
  • See answer by Contango at https://stackoverflow.com/questions/26193365/pycharm-does-not-recognize-modules-installed-in-development-mode/ – Contango Sep 18 '17 at 08:38

2 Answers2

1

I couldn't reproduce your error (PyCharm 5.0.4, OS X 10.10.5, Python 3.4.3/2.7.6). You could try run this code in a console to find out the current working directory, and if it's not the same as filepickle's one, most likely it is the problem.

import os
os.getcwd()
Maxim Kuleshov
  • 146
  • 2
  • 8
  • Hi, Thank you for your response. If I run os.getcwd() from the Pycharm Python console I get this as my current directory: – user3798654 Mar 09 '16 at 20:50
  • 1
    Hi, Thank you for your response. That was it. When I created this project in PyCharm I created a directory to keep all my modules in. When I took the module out of that directory and moved it up to the Pycharm working directory the import works as expected. Thanks for your help, it is much appreciated. – user3798654 Mar 09 '16 at 20:57
0

I had exactly the same problem.

The solution is to mark the module directory as "Source Root", and also configure Python to import "Source Root" directories.

For screenshots on how to do this, see the answer from Contango here:

PyCharm does not recognize modules installed in development mode

IMHO, this is a bug in PyCharm: this should just work, and it shouldn't require two separate steps to get anything working.

This issue is present in PyCharm Community Edition 2017.2.3 on Windows 10 (and probably Linux as well).

Contango
  • 76,540
  • 58
  • 260
  • 305