JupyterLab - import module - python-3.x

I use JupyterLab, jupyter notebook and try to import the script. The script is located in the notebook's directory.
Unfortunately import is impossible.
Error:
ImportError: No module named 'irt_01_generate_sample_data'
On the other hand:
When I run terminal and import the script, everything works fine.
What should I do to import the script ?

One thing to check is whether you're running the same Python (executable) and have the same paths (where it is looking for imports) in both the terminal and Jupyter Notebook:
import sys
sys.executable
sys.path

Related

Run test in separate file with unittest.main

I create a notebook which looks like the following:
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
unittest.main(argv=[''],exit=False)
When running the notebook, everything works fine. Now, I try to move the test out to a separate *.py file:
Notebook code:
import unittest
unittest.main(argv=[''],exit=False)
New Python file (in same repo):
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
When running the notebook I always get
Ran 0 test
I tried to add the path as argv such as
unittest.main(argv=['/Workspace/Repos/XXX/repo'],exit=False)
but the *.py file is not discorved
What do I have to hand over to argv so that the py file is identified
Thank you Daniil for the link:
import unittest
unittest.main(module="simple_test",argv=[''],exit=False)
makes the job!

Pyinstaller with anaconda in spyder

So i wrote a little programm in python with use of tkinter. The repo is found here: https://github.com/Odatas/MeisterTools
I now want to create an exe so people only need to use the exe when they want to use the program. But the exe i create with pyinstaller doesnt work and throws the error:
Import Error: cannot import name 'travel' from 'main'
The cmd i opend to creat the exe is directly out of anaconda envoirment.
I cd into the folder where all the scripts are and then run it like this:
pyinstaller --onefile patrickstools2.py
I even tried to make every import an hidden import:
pyinstaller --onefile --hidden-import=init --hidden-import=main --hidden-import=checker --hidden-import=contact --hidden-import=dangers --hidden-import=droptable --hidden-import=importexcel --hidden-import=odatasfunctions --hidden-import=randomenpc --hidden-import=scrolltest --hidden-import=sonstiges --hidden-import=travel patrickstools2.py
that doesnt help either. I added the path through Anaconda to the PYTHONPATH variable...so it should be know in any way shape or form.
The complet code is in Anaconda. The Error gets thrown in the import section of the main file:
# page classes import
import os
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
# page classes import
from . import travel
from . import contact
from . import dangers
from . import sonstiges
from . import randomenpc
I allready created an exe with pyinstaller from a previous version. But i made some changes to the structure of the programm. The run.py is only there because i work with spyder and as far as i know spyder needs it because else the import doesnt work correctly.

Python3 numpy import error PyCapsule_Import could not import module "datetime"

I am trying to import numpy with python3 on MacOS mojave. I am getting this error. I don't know if it has something to do with a virtual environment or something like that.
Error:
PyCapsule_Import could not import module "datetime"
I have tried reinstalling python3 and reinstalling numpy
In my case I had this problem, because my script was called math.py, which caused module import problems. Make sure your own python files do not share name with some of common module names. After I renamed my script to something else, I could run script normally.

How do I import modules that are used inside a Python 3 file?

I have the following code in myfile.py
def show_path():
print(os.getcwd())
In Jupyter notebook, I have the following (which runs fine):
import os
from myfile.py import show_path
However, when I run the following:
show_path()
I get 'name 'os' is not defined' error. But when I simply type:
os.getcwd()
I get the path, which I understand. But I don't understand why running show_path() doesn't do the same thing? Is it necessary to have import os inside my myfile.py file? If so, why?
You have to import os in your module, its import list is different then that of your notebook. Since you did not import it in your module, it wa unavailable when your module was parsed.

Trying to import winsound

When I import winsound and then try to run the program, it returns an error message saying:
ImportError: No module named 'winsound'.
Are there any settings I need to change?
winsound only exists in Python installed under Windows. Do not attempt to import it if you are not running under Windows.

Resources