Python import error in Jupyter Notebook Pycharm 2019 IDE - python

I recently installed PyCharm 2019 professional edition in Windows 10. I created a new Project 'Sample' and two files 'file1.py' and 'file2.ipynb'. I have installed jupyter notebook from the chosen python interpreter.
class Foo is defined in file1.py
I then import file1.py into file2.ipynb to use Foo
Here, I encounter a strange error. 2019 professional edition has local notebook server installed in it.
from file1 import Foo
I ran the above code sample in a cell both in editor inside PyCharm IDE as well as in browser, after turning on the jupyter server on localhost.
For some reason, the code sample throws out import error problem in IDE but runs smoothly in browser. I tried looking if there are any issues with project path but couldn't figure out the reason yet. Anyone encountered this before ?
Here is the screenshot of Import Error:

os.getcwd() helped me understand the problem. The jupyter notebook editor is running under Main directory 'Sample'. Whereas my files are in 'Sample/Resource/file1.py', 'Sample/Resource/file2.ipynb' .
Now If I import using the following commands in file2.ipynb file, it worked:
import os
print(os.getcwd())
#%%
from Source import file1
print(file1.Foo())
Thank you very much for the help #Vishal #IonicSolutions

Related

Why can't I import my files in VSCode in python?

This morning I tried to import a file that I created into another file but the file name that I am trying to import has the yellow scribble line under it. I know this is an import error but I can't fix it. I am working in VSCode on Windows 10 in python. The files are in the same directory.
I tried to import the path of the file but it doesn't work.
I had an old working file which requires me to import another file into it but now it doesn't work as well. Is this a new bug in VSCode?
Any help would be appreciated.
you can try : path/to/python3 -m path/to/script.py
or just specify the full path : 'C://Users/quina//...//word.txt'
Maybe try to use the normale python3 IDLE or throught the terminal just to understand you error.
There is no word.txt in 'Visual Studio Projects' folder

VS Code doesn't switch between Anaconda environments correctly

I have:
two conda environments, myenv1 and myenv2;
two folders containing .py files: folder1 and folder2.
Let's say I've been working on folder1 in VS Code using myenv1, and then I do the following:
I close folder1 (using File -> Close Folder)
I open folder2 (using File -> Open Folder...).
I switch to env2 using the Python: Select Interpreter command.
What happens?
The blue information bar at the bottom of the VS Code window says "Python 3.8.2 64-bit ('env2': conda)" -- this suggests to me I'm now working in env2.
But I don't think the VS Code has correctly switched to env2. Why? Because if I attempt to import a package that exists in env2 but not env1 I get an error. If I import a package that exists in env1 but not env2 the import is successful.
But if I close VS Code and open it again (without doing anything else), the switch to env2 has been successful (by performing the same import test).
Is this a known bug, or am I doing something wrong? I've tried to find the issue on github without success.
Many thanks for your help.
Frank.
PS: I guess I could manually set sys.executable but I'd like to use the VS Code functionality if possible.

Launch Jupyter Notebook with launcher and basic code in Linux Mint

I currently open Jupyter Notebook by running jupyter notebook in my terminal.
I have 2 questions that relate to a common goal.
1) How do I create a desktop icon (i.e. launcher) to start Jupyter Notebook on my Linux Mint computer?
2) I want to run some basic code upon double-clicking this launcher:
import numpy as np
import pandas as pd
import seaborn as sb
import sklearn as skl
I don't want to type this code whenever I open Jupyter Notebook, so it would be nice to automatically run it.
For #1, I'm trying to follow the steps here:
https://forums.linuxmint.com/viewtopic.php?t=256156#p1382045
However, I don't know where to find the path for the "Command" field. I tried browsing the Anaconda folder on my computer, but I can't find Jupyter Notebook there.
For your first question, if you open up a terminal, you can find where programs are with which, like so
which jupyter
This should output where your particular jupyter is being called from.
For your second question, it looks like you can create a profile to start up certain functions.
In bash, you can run the following
# Create a new folder if it already doesn't exist
mkdir -p ~/.ipython/profile_default/startup
# Create Python file to put your favorite imports
touch ~/.ipython/profile_default/startup/start.py
So the start.py (that is located in ~/.ipython/profile_default/startup/) is where you can put your imports. So this start.py file should contain
import numpy as np
import pandas as pd
import seaborn as sb
import sklearn as skl
Resources:
https://towardsdatascience.com/how-to-automatically-import-your-favorite-libraries-into-ipython-or-a-jupyter-notebook-9c69d89aa343
https://stackoverflow.com/a/11124846/
https://ipython.readthedocs.io/en/stable/interactive/tutorial.html#startup-files
In sum, if you implement both of the suggestions above, you can get both an icon to auto-load your favorite Python library packages as you start a Jupyter Notebook.
Warning: typically for reproducibility and transferability of Jupyter Notebooks to others, I would go against auto-loading libraries into your Jupyter Notebook. I understand the repetition of having to load the same libraries you use all the time, but if for whatever reason, you change computers or a colleague needs your code, then your notebook will not work correctly. Just my two cents to keep in mind if/when implementing this.

Python Modules & Packages using jupyter notebook in Google Cloud Platform

I'm using Jupyter Notebooks within Google CloudDatalab in Google Cloud Platform for my Python scripts. This creates .ipynb files.
I want to be able to create Modules and Packages using Jupyter Notebooks but it is not able to import the scripts.
For e.g.:
mymodule.ipynb has this:
def test_function():
print("Hello World")
Then in myscript.ipynb when I try to import the above:
from mymodule import test_function
It throws an error :
*ImportErrorTraceback (most recent call last) in ()
----> 1 from mymodule import test_function ImportError: No module named mymodule*
How do I create Modules & Packages using Jupyter Notebooks?
Notebooks can't be used as modules. You need to create a python file (e.g. mymodule.py).
If you want you can do this from within a jupyter notebook:
with open('mymodule.py', 'w') as f:
f.write('def test_function():')
f.write(' print("Hello World")')
from mymodule import test_function
test_function()
# Hello World
You cannot import Jupyter Notebook in the same way as Python files (packages).
Check this link: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html
I've received a response on a different forum which answered my question. Here is the answer:
Put your module code into a .py file and make sure that's in the same directory as myscript.ipynb.
To create the .py file from the code currently in a notebook, you can download it from within Jupyter as a .py file and tidy it up in your text editor of choice.
Remember, the import statement is pure python syntax. It knows nothing about - and has no wish to know anything about - jupyter notebooks. It's looking for a .py file.
This has resolved the issue.
Thanks a lot.

import statement is not working when running python script from the command line

I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).
I used PyCharm (community edition) to write the script and it is working from inside the IDE.
I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:
from package_name,file_name import ClassName
The Error message in the command line:
ImportError: No module named 'package_name'
if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.
What worked fine for me is:
Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.
for example ,I was running the script file from conf path and my script was importing module situated in \scripts\Setup\ so appended the path of module like below.
import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup')
then use import statement of module for which issue was thrown.
I found the answer for my question above, and the problem was much easier than I thought.
Addressing the Problem
having many python packages in different directories
your script needs some/all packages, that are not in the standard lib-directory for your python installation (e.g.:prefix/lib/pythonVersion).
Solution
Short term solution
As long you are using an IDE (e.g. PyCharm), it is sufficient within the code to add:
import sys
sys.path.append("path/to/package")
As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.
Better solution
Add the directories of your packages and of your python installation to your shell-profile(e.g.: .bashrc) using the command:
export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages
To get more info about PYTHONPATH, check this link
In this case you will not need to append the path of your packages within your code :)

Categories