How to make executable Python file from .ipynb (Jupyter Notebook)? - python

I wrote a Python program in Jupyter Notebook. The program uses libraries installed through Anaconda. I need to get a separate executable Python file that would work on forks of Ubuntu and Debian.
I created the .py file from the .ipynb file through the menu in the Jupyter Notebook:
File -> Download as -> Python (.py).
Next, I try to run .py file through the Terminal in Linux:
>>> python3 name_of_created_file.py
And I get an error:
Traceback (most recent call last):
File "name_of_created_file.py", line 11, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
As I understand it, there are not enough software libraries to run the program. On the same computer (Linux) in Jupyter Notebook, my program works well.
How to get a working program separately from Jupiter Notebook? To do this, I need to separately install software libraries?

Can you please check the version of python in your local, whether python or python3. Your issue might be because of two different python versions.
Depending upon your version of python in local machine, you might have to use the same version of pip to install pandas.
Check the version of python you are running in Jupyter.

Related

ModuleNotFoundError in VS code with Jupyter note book but not .py files

I am using visual studio code on windows, and I am trying to run a python code on jupyter-notebook. I have all packages installed and they work fine when it is a normal python file. But I need it to run as a notebook. Once I run the block, I immediately get the following error:
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_5668\1439934476.py in <module>
1 import csv
----> 2 from pandas import read_csv
3 from matplotlib import pyplot as plt
4 import numpy as np
5 import os
ModuleNotFoundError: No module named 'pandas'
I made sure that requirements are already satsified for this module, and I already tried running this line in python file with no issues. Therefore, how can I solve this issue? How can I let Jupyter notebook compiler to see where these packages actually are?
The installation of pip is related to the python path specified in the environment variable. You need to select the correct interpreter in vscode.
Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
From within VS Code, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
Read docs for more details.
First you need to install pandas by running the command in your console:
pip install pandas
If you don't have pip installed on your computer then install it, otherwise you won't be able to download pandas or other modules
PS: check that when you downloaded python you chose the add to PATH option
Hoping I could help
If you have a problem running jupyter notebook file make sure
You have installed jupyter notebook from anaconda from this site
Once you installed that, open anaconda navigator and click environment tab
Install python and pandas
Jupyter Notebook works like environment different from VSCode
You can also run Jupyter Notebook file on VSCode by installing Jupyter extension in VSCode
My guess is you don't installed it yet in anaconda navigator, that's what makes it error

Unable to import pandas on VScode and Atom IDE

I am really frustrated right now and have tried so many workarounds all to no avail. I want to do some analysis on a csv file and I learnt that I would need to import pandas.
So I installed python version 3.10.5 and ran this code in terminal:
pip3 install pandas
This successfully installed pandas on terminal in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
I ran this code on my Atom IDE and VScode too:
import pandas as pd
This was the error:
[Running] python -u "/Users/augustine/Downloads/pythonscripts/tempCodeRunnerFile.python" Traceback (most recent call last): File "/Users/augustine/Downloads/pythonscripts/tempCodeRunnerFile.python", line 1, in <module>
import pandas as pd
ImportError: No module named pandas
I saw on reddit that only python 3.9 supports pandas for now. I uninstalled version 3.10 and installed 3.9, on VS code, I changed the python interpreter to 3.9 version but it's the same thing happening. Please what am I supposed to do?
It's good practice that every time you make a new project involving python, to change to that project's base directory and then run python -m venv venv to set up a stable python environment there, when you use it you run ./venv/Scripts/activate, and then all dependencies can be managed in a requirements.txt file. The added bonus is if u do that, vscode will see it and then auto-suggest whether you want that to be the default interpreter for your project code. If that doesnt happen, try the magic "ctrl + shift + p" and then start typing "python interpreter" -- an option will appear for you "Python: select interpreter" and then just use the one you've created in the venv (./venv/Scripts/python.exe).
I think this is related to the existence of multiple Python in your environment. Try to manually specify the python location in setting.json:
"python.defaultInterpreterPath": "C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",#your python path

OSQuery and Python extensions with virtualenv

I'm using OSQuery throught osqueryi and/or osqueryd on Windows.
I've written some Python extensions (tables) and I try using virtualenv to run these Python extensions.
When I run osqueryi.exe and python extensions separately from command line, the extensions are loaded ok and I can query my python tables. In this scenario, I use virtualenv ok.
When I use extensions.load, with my Python extensions and
osquery.flags has the next content
--disable_extensions=false
--config_path=C:\ProgramData\osquery\osquery.conf
--config_plugin=filesystem
--logger_plugin=filesystem
--logger_path=C:\ProgramData\osquery\log
--extensions_autoload=C:\ProgramData\osquery\extensions.load
In this scenario, osqueryi.exe shows the next error
Traceback (most recent call last):
File "C:\ProgramData\osquery\extensions\my_table.ext", line 3, in <module>
import osquery
ImportError: No module named osquery
First, I activate my virtualenv and later I run osqueryi.exe with this osquery.flags file
In both scenarios, I used the same virtualenv environment with external modules installed via pip.
How can I configure OSquery To use virtualenv with Python Extensions.
Thanks
I have solved this issue.
Looking source code I have seen an environment variable called OSQUERY_PYTHON_PATH
In Windows you should run something like this
set OSQUERY_PYTHON_PATH=<path to python.exe in virtualenv>

Multiple programs installing python modules on computer, confusing the crap out of IDLE

Most of the programs I use for my work have independent python API's which do various things. The downside to this is that it appears that each program installs its packages wherever it wants. This is by way of introduction.
Now, I am trying to run matplotlib through IDLE. I ran a clean pip upgrade and the ran a clean install of matplotlib using pip in cmd. The commands I used were found in this document, page 13.
When I try to import matplotlib in IDLE, I get the following error message:
>>> import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
File "C:\Program Files\ParaView 5.2.0-Qt4-OpenGL2-Windows-64bit\bin\Lib\site-packages\matplotlib\__init__.py", line 727
except Exception, msg:
The path it points to is for an independent program named Paraview which has its own set of python modules. It looks like Paraview also has a definition for matplotlib but I don't want to use this one. A quick snip of the paths in IDLE shows:
It looks like IDLE is prioritizing the Paraview folder over the others. How do I get IDLE to disregard this folder and look into the matplotlib module I just installed by default? Also how do I prevent this unfortunate and seemingly unnecessary duplication of modules?

dll load errors in python executable made from pywin32

I created python executables with py2exe with both 64bit python interpreter and 32 bit python interpreter.
In my program, I use the module pywin32 com, and so I dl'ed and installed both the 64bit and 32bit versions of the program prior to creating the executable.
The 64bit exe works fine, but the 32 bit one has the following problem:
Traceback (most recent call last):
File "program.py", line 11, in <module>
File "win32com\__init__.pyc", line 5, in <module>
File "win32api.pyc", line 12, in <module>
File "win32api.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be found.
I tried both versions by running the source directly, ie python program.py and C:\python_32\python.exe program.py and both work fine.
Note I tested the 64bit exe on a 64bit windows 7 computer and the 32bit exe on a 32bit windows XP computer.
Older than everything ever, but I stumbled on this problem today, and if anyone else does, what I ran was:
python /c/Python26/Scripts/pywin32_postinstall.py -install from the commandline (change values to fit). That worked:
$ python /c/Python26/Scripts/pywin32_postinstall.py -install
Copied pythoncom26.dll to C:\WINDOWS\system32\pythoncom26.dll
Copied pythoncomloader26.dll to C:\WINDOWS\system32\pythoncomloader26.dll
Copied pywintypes26.dll to C:\WINDOWS\system32\pywintypes26.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\2.6\Help[None]=None
-> Software\Python\PythonCore\2.6\Help\PythonwinReference[None]='c:\\Python26\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Creating directory c:\Python26\Lib\site-packages\win32com\gen_py
Shortcut for Pythonwin created
Shortcut to documentation created
The pywin32 extensions were successfully installed.`
(On WinXP SP3)
Had the above noted errors with my python27 pywin32 installation that got installed after my installation of ArcGIS on a windows2008 R2 server (trying to centralize our scripts).
Found that for me, I had to start a command line window with run as adminstrator (right click to get that option in windows) and then run the following command line from the c:\arcpy27\arcgis10.1>python c:/arcpy27/arcgis10.1/scripts/pywin32_postinstall.py -install
I see a lot of people have run into this issue and I tried multiple things, but this was finally the solution that managed to get pywin32 installed.
Thanks so much for your solution. Took me a couple of install and uninstalls before I finally ran across your solution and modified it for my version.

Categories

Resources