Import module not found error
python 3.x - Import a javascript function in js2py - Stack Overflow
How to import javascript code in python with variable? - Stack Overflow
jupyter notebook - "ImportError: No module named" when trying to run Python script - Stack Overflow
There is a new alternative to js2py called PythonMonkey which lets you import JavaScript files directly from Python and execute them.
https://github.com/Distributive-Network/PythonMonkey
JavaScript numar.js file:
module.exports = function numar(){
return 1; //return 1 if the checkbox is
}
Python main.py file:
import pythonmonkey as pm
numar = pm.require('./numar')
print(numar()) # this outputs "1.0"
This is still an issue in js2py and this link may help you. https://github.com/PiotrDabkowski/Js2Py/issues/123#issuecomment-866218046
The only way to solve this is to modify translated file.
# translate myfunction.js, numar.js files to python
import js2py
js2py.translate_file('numar.js', 'numar.py')
js2py.translate_file('myfunction.js', 'myfunction.py')
// myfunction.js
var numar = require("./numar");
function myfunction() {
var b = numar();
return b;
}
// numar.js
function numar(){
return 1; //return 1 if the checkbox is
}
# then modify translated myfunction.py file like this
__all__ = ['myfunction']
# Don't look below, you will not understand this Python code :) I don't.
from js2py.pyjs import *
from numar import numar
# setting scope
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers(['numar', 'myfunction'])
@Js
def PyJsHoisted_myfunction_(this, arguments, var=var):
var = Scope({'this':this, 'arguments':arguments}, var)
var.registers(['b'])
var.put('b', var.get('numar')())
return var.get('b')
PyJsHoisted_myfunction_.func_name = 'myfunction'
var.put('myfunction', PyJsHoisted_myfunction_)
var.put('numar', numar.numar)
pass
pass
# Add lib to the module scope
myfunction = var.to_python()
# import in your code
from myfunction import myfunction
print(myfunction.myfunction())
You can find the code here.
https://github.com/lacmansoftware/python-js2py-require-solution
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.
Just create an empty python file with the name __init__.py under the folder which showing error, while you running the python project.
Hey there! I've been getting so frustrated trying to figure this out. I keep getting ModuleNotFound errors for modules that I most definitely have installed, this case requests. I tried moving the install around and making sure it was in the same folder I'm working on my python file from but to no avail. I'm sure I'm missing something stupid. The traceback error is as follows:
Traceback (most recent call last):
File "/Users/redacted/Desktop/cxt_copy/zd_test.py", line 5, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
I'm relying on this for work so any help is much appreciated!
EDIT: I'm on a Mac, using Sublime Text Editor to run Python, have Python3 installed and I installed requests using pip install via the terminal.