Pycharm Error: 'No Module' When Trying to Import Own Module (Python Script)

Python error ImportError: No module named

Based on your comments to orip's post, I guess this is what happened:

  1. You edited __init__.py on windows.
  2. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
  3. You used WinSCP to copy the file to your unix box.
  4. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
  5. The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
  6. You create __init__.py in the appropriate directory and everything works... ?

Cannot import module from source directory in pycharm

For some frigging reason i don't understand setting the src directory as my source sources root instead of the project_title directory (which contains it anyways...but what do I know) worked.

So solution was:

-> settings
-> project structure
-> remove previous content root path and make content source the src directory

import like this:

from pipeline_tools.helpers import func_1, func_2

Note: that i do not have a __init__.py file in my folder, as I believe i read they're not necessary anymore.

Prior to Python 3.3, filesystem directories, and directories within zipfiles, had to contain an init.py in order to be recognised as Python package directories. Even if there is no initialisation code to run when the package is imported, an empty init.py file is still needed for the interpreter to find any modules or subpackages in that directory.

This has changed in Python 3.3: now any directory on sys.path with a name that matches the package name being looked for will be recognised as contributing modules and subpackages to that package.

From Nick Coghlan’s Python Notes

Meet import error in terminal, but PyCharm can run it

pycharm adds your project directory to the PYTHONPATH environment variable (you could add other folders with Settings->Project Structure->Add Content Root).

Outside of the virtual environment of pycharm your project was not set to any search path.
Two options:

  1. Append project path to PYTHONPATH environment variable (either create new environment variable PYTHONPATH or add path with ";" to existing variable)
  2. use sys.path.append:
    add following lines
  import sys
sys.path.append(r"../project")

before import datasets in train.py

Pycharm ModuleNotFoundError: No module named // python import from child directory fails

If you look into PyCharm configuration, there are two options:

  1. Add content roots to PYTHONPATH
  2. Add source roots to PYTHONPATH

They are flagged by default.

In your case, the first one allows you to run the script correctly because it adds root_project path in the PYTHONPATH environment variable.

So, if you want to run the script also in command line you should set the same variable.

You can proceed like this:

  1. Open command line

  2. If you are on Linux you can use

    export PYTHONPATH=<absolute_path_of_root_project>

  3. If you are on Windows you can use

    SET PYTHONPATH=<absolute_path_of_root_project>

  4. Run script

    ~/Desktop/root_project/subfolder » python3 sub_script.py

Please remember that the export/set command is not permanent, it is valid for the current command line session.

Python: 'ModuleNotFoundError' when trying to import module from the same package

In fact, the file games.py and lock.py and pointClass.py is in the same folder so why not importing it directly like this :

from lock import RWLock
from pointClass import *

import another subpackage from a subpackage doesn't show docstirng in Pycharm

After days of research, I found the answer.

Solution 1 :

Do from src.folder2.script2 instead of from folder2.script2 because I'm opening the project from the parent directory of src.

Solution 2 :

Set the source directory of the project as src as it currently is the parent directory of src.
Sample Image



Related Topics



Leave a reply



Submit