Google Colab, module not found when running python script - python

I have a Colab notebook open and have my files cloned from a github repo. I want to run a python script called models.py. In this file, I am using pandas. When I run this line in Colab:
!python3 models.py
I get the following error:
Traceback (most recent call last):
File "models.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
However, if I run in a cell on Google Colab:
!pip3 list
I find that pandas is indeed installed:
pandas 0.25.3
My assumption is that when I run the script, it is not able to see the libraries I have installed but I am unsure how to fix this issue.
If I run:
!which python3
I get:
/usr/local/bin/python3
The python file I am trying to run is under:
/content/my_project/models.py
Should I instead take a different approach to running this file?

Instead of
!python3 models.py
You can use
%run models.py

In a clean Colab runtime, the location of python 3 is different than what you show in your question:
!which python3
# /usr/bin/python3
It looks like you are installing another Python instance in your VM. To ensure you're using Colab's bundled Python 3 executable, try using
!/usr/bin/python3 models.py
If you actually want to use the new python instance you've installed, you'll have to also install whatever packages you need. For example
!/usr/local/bin/python3 -m pip install pandas

Related

No module named 'utils.utils' in Google Colab

I'm trying to run this python code in Google Colab and I always get this error that the utils module is not installed or does not exist
yet I've ran !pip install utils and the still the same issue.
I've tried running it on my computer and it works without issues but I can't actually run it due to limited resources of my pc.
anyway anyone has a solution for this ?
Traceback (most recent call last):
File "/content/GNN-for-text-classification/preprocess/build_graph.py", line 15, in <module>
from utils.utils import loadWord2Vec, clean_str
ModuleNotFoundError: No module named 'utils.utils'
I am assuming you are using this GNN-for-Text-Classification.
Now, you have probably cloned the repository in your local system and you're running the .py files from there.
But, a New Notebook in Colab will not have the files that you have cloned/downloaded. So, when you're doing
!pip install utils
The utils package from Pypi is getting installed which doesn't contain the functions you require.
What you need is actually the utils module from GNN-for-Text-Classification, for which you'll need to clone and cd into the folder in Colab itself. Just run the following in your Colab Notebook:
!git clone https://github.com/zshicode/GNN-for-text-classification.git
%cd GNN-for-text-classification/
%ls
This will clone the repo, cd into the folder and view the contents where you will be able to find the utils module that you need.
Now you can import stuff like loadWord2Vec, clean_str without any errors.
Note that this cloning is not permanent since a new Colab instance will not keep the changes from the old one.

How to run the python webapp using flask & anaconda from vscode?

I have installed Anaconda-Package, Flask, Pip & Vscode. I am currently running the development project on localhost using "pipenv shell" command then "flask run" command in vscode.
It works well on http://127.0.0.1:5000/ for simple webapp. But I want to display the prediction charts & tables on a webpage using pandas, numpy etc.
So, When I try to import
import pandas as pd
import numpy as np
It displays this ->
flask.cli.NoAppException
flask.cli.NoAppException: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "c:\users\mdev\.virtualenvs\flask_md_project-itylmdvv\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "D:\flask_md_project\app.py", line 4, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'
above error as image
How do I use pandas,numpy,matplotlib etc from anaconda package along with flask through vscode for my python webapp ? Please help 🙏
According to the information you provided, the modules "pandas" and "numpy" are not installed in the Python environment you are currently using.
When we use the conda environment in VS Code, we use it as a python environment. Therefore, in this environment we need to install the module and then use this module. Usually, the module storage location of conda environment is (for example, base conda environment): "\users\username\anaconda3\lib\site-packages".
We can use the command "pip show pandas" in the VS Code terminal to check the installation location of the module:
For more information about flask in VS Code, you could refer to: Flask Tutorial in Visual Studio Code.
Install numpy on anaconda by running;
conda install numpy

"ModuleNotFoundError : No module named 'apiclient ' "

I recently tried to view Google Calendar using python . To do that I referred Python Quickstart. I did the steps that were mentioned in the website.
I completed the second step and downloaded the secretclient.json file.
In the terminal I typed in :
pip install --upgrade google-api-python-client
as I was instructed in the website.
I ran the python that was on the website and when I compiled I got the error:
Blockquote
Traceback (most recent call last):
File "quickstart.py", line 2, in
from apiclient import discovery
ModuleNotFoundError: No module named 'apiclient'
The Lines which correspond to the error are :
from apiclient import discovery
Why is the apiclient module unavailable ?
Could it be that you're using a different python version than what the pip installed? For example, if you use python3 to execute the problematic import line, but pip is for python2. Or if you use conda or another python distribution that uses a different path to import the packages from.
You can verify it if you just open from the command line:
python
then
from apiclient import discovery
and check if you still get the error.
you can resolve this by going to Script folder of your Python installation directory and running from there
e.g.
cd D:\Python27\Scripts\
python
from apiclient import discovery
Mainly this issue arises when u have more than one python installation , as noob have suggested

"cannot import name smbserver" when using impacket with smbrelayx.py

I know this is not an actual info-sec question, but I am having problems with getting the smbrealyx.py module to work. For some reason I get the following error when I try to execute the aforementioned python program.
Traceback (most recent call last):
File "smbrelayx.py", line 43, in <module>
from impacket import smbserver, smb, ntlm, dcerpc, version
File "/usr/lib/python2.7/dist-packages/impacket/smbserver.py", line 18, in <module>
from impacket import smbserver, version
ImportError: cannot import name smbserver
I am not familiar with python programming and I was hoping someone could help me fix this issue.
Looks like you have an old impacket version installed and you are using a newer version of smbrelayx.py.
First of it'd be great to know what version you have. You can easily do that by typing inside a Python interpreter the following:
from impacket import version
print version.BANNER
Assuming you have an old version, first of all it'd be great to remove the existing version. Depending on your Unix distro it might be just as easy as to remove the python-impacket package, or you can manually remove the library files by getting to know where those files are located:
import impacket
print impacket.__file__
That will give you the path where the library is installed. I'd suggest to remove the entire directory.
Now that your system is clean, you have two options:
Install a stable version: Grab the latest stable version from here. Uncompress it in a temp directory and then run:
python setup.py install
That will install the libraries and example scripts (e.g. smbrelayx.py)
Install the development version: You will need to git clone the development version first by running:
git clone https://github.com/CoreSecurity/impacket
Once the repo is cloned, inside the impacket directory type:
python setup.py install
That will install the libraries and example scripts (e.g. smbrelayx.py)

Error to launch IPython shell after source install

I have an account in a CentOS server without sudo permission. As a result, I tried to install IPython from source code by python setup.py prefix=<my home directory>.
However, I got the Error:
Traceback (most recent call last):
File "/usr/bin/ipython", line 19, in <module>
from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp
I found a question same to mine: IPython import failure and python sys.path in general.
I followed the asker's instruction to add the directory of my IPython to the IPython execution file.
#!/usr/bin/python
"""Terminal-based IPython entry point.
"""
import sys
sys.path.append("./Ipython directory")
from IPython.frontend.terminal.ipapp import launch_new_instance
launch_new_instance()
However, I got the same error as well. So I want to know how can I launch IPython correctly? Thanks.
You probably want to install with the --user flag, which should put it on sys.path automatically.
python setup.py install --user
If you need to use --prefix for some other reason, then make sure that the directory you add to sys.path is the folder containing the IPython package, not the IPython directory. It will probably end with 'site-packages'.
I copied the IPython source folder into ~/bin/. The execution file of IPython can recognize the module in IPython and the IPython shell launches successfully.

Categories

Resources