"Importerror: No Module Named" When Trying to Run Python Script

ImportError: No module named when trying to run Python script

This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run

import os
os.getcwd()

you'll see this is true.

However, let's say you're using an ipython notebook, run os.getcwd() and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir setting).

The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:

import sys
sys.path.append('my/path/to/module/folder')

import module_of_interest

If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.

The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.

Python - No module named my_module in terminal, but not in PyCharm

The PYTHONPATH in your terminal environment doesn't contain 'my_module'.

Configure the PYTHONPATH to include the directory containing your module

It works in pycharm because it sets up the path for you automagically.

Learn about the module search path

Can't run python script from command line NO MODULE NAMED project_name

description_extractor is not a python file (note it does not have the .py extension). To launch it from the command line you will need to try launching a python file.

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... ?

How to fix ImportError: No module named ... error in Python?

Python does not add the current directory to sys.path, but rather the directory that the script is in. Add /home/bodacydo/work/project to either sys.path or $PYTHONPATH.

Relative imports - ModuleNotFoundError: No module named x

As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)

ModuleNotFoundError: No module named 'xlwings' when trying to run a Python script out of a VBA macro in Excel

I replaced the previously set VBA code by the one below and it did the trick.

Sub FX_Lista_Pares_POST()

Dim objShell As Object
Dim PythonExePath As String, PythonScriptPath As String
ActiveWorkbook.Save

Set objShell = VBA.CreateObject("Wscript.Shell")

PythonExePath = """%USERPROFILE%\AppData\Local\Programs\Python\Python310\python.exe"""
PythonScriptPath = """%USERPROFILE%\OneDrive\Jobs\Consulting\Data BI\Cases\FinData\FinMkt\RapidAPI_Invest_FX_Lista_Pares_POST_Req.py"""

objShell.Run PythonExePath & PythonScriptPath

ActiveWorkbook.Connections("Query - FX_Lista_Pares_POST_Req(1)").Refresh
Range("A4").Select

End Sub

I attempted to solve this issue by using this same VBA script, but I had done so with its scope restricted within a respective worksheet, and that's probably the reason the issue kept persisting. Now I have done it within a module and everything works just fine.



Related Topics



Leave a reply



Submit