bash: Python import - Command not found for pandas - python

I am a beginner in Python using MacBook
I want to import pandas in my Python script and I'm typing the following command below:
import pandas as pd
which results in:
Error: -bash: import: command not found
Questions:
How can I enable import command. I used #!/usr/bin/python and #!/usr/bin/env python3 as well but nothing happens after hitting enter.
I am importing pandas in a folder under Documents. I hope that's OK. I can't put the path where my Python is installed, since it is in Applications folder.

Sounds like you need to open the Python prompt first.
Try this:
$ python
>>> import pandas as pd
Where $ is the prompt in your bash shell, and the >>> is the prompt in your Python prompt. Don't type these in.

I think you are running python code on shell script. I tried to get the same result by running shell script with python "import command" in it.
I created a file test.sh
#!/usr/local/bin/python
import pandas as pd
and the output was: test.sh: line 3: import: command not found
You need to run it as "/usr/local/bin/python test.py" or just "python test.py"

Related

ImportError when importing in python notebook, but import works fine in terminal?

The import works when I call it from the command line, but when I call the import in the ipynb I get the following error:
ImportError: libmkl_intel_lp64.so: cannot open shared object file: No such file or directory
I have already added the path to libmkl_intel_lp64.so to my bashrc file, so I am not sure what else I can do to fix this import error in the notebook.
Your command line python interpreter,
and your jupyter notebook kernel,
have different environments.
Use one of these techniques to help you debug the path problem:
$ python -m site (from bash prompt)
import sys; from pprint import pp; pp(sys.path) (within a cell)
Do yourself a favor, and use conda to manage your environment.
https://docs.conda.io/en/latest/miniconda.html
Be sure to conda activate myfavoriteenvironment
before you start the jupyter kernel.

pybind11 module cannot be found in Jupyter, but can be found in command line python and Spyder

Folks, I am having this puzzle with a pybind11 library mytest.cp37-win_amd64.pyd put in C:\Temp.
Then I have this:
import sys
sys.path.insert(0, r"C:\Temp")
from mytest import *
Now here comes the problem, if I launch the python in command line:
>python
>>>import sys
>>>sys.path.insert(0, r"C:\Temp")
>>>from mytest import *
It works fine. Or I just put above code in test.py, then run:
>python test.py
It also works. If I put this piece of code in Spyder, it works as well. But if I put this piece of code in Jupyter, it will not work by saying:
ModuleNotFoundError: No module named 'mytest'
I am sure all my tests are conducted in the same python environment by printing it out:
import os
print(os.environ['CONDA_DEFAULT_ENV'])
Am I missing anything here?
With Wayne's help and a previous post, I finally found out the root cause, that is the version of the python running in the Jupyter kernel is different from that of the environment from which the Jupyter is launch and also the pybind11 library is build with.
After I reinstall the Jupyter in the environment again, the library is picked up successfully.

Running bash/python script with ProcessBuilder

I want to run a bash script by using ProcessBuilder. This is my xtend code:
new ProcessBuilder().inheritIO().command("/bin/bash", "-c", "./myscript.sh")
This is my bash script:
#!/bin/bash
python WebRoot/result.py
And the python code:
#! /usr/bin/env python
import rospy
from std_msgs.msg import Empty
...
The problem is that I get an error:
Traceback (most recent call last):
File "WebRoot/result.py", line 2, in <module>
import rospy
ImportError: No module named rospy
However, when I run the code manually via terminal, it works fine.
When you run it command line, you are probably getting a different environment from when it's running in your JVM. In your bash script, try pointing directly to the python version you intend to use. It's entirely possible that the JVM's env is pointing to a different version of python. Or that the environment is not fully setup.
Try to put the full path, like this, for example:
#!/bin/bash
/usr/bin/python2.7/python WebRoot/result.py

python3 - ImportError: No module named twython

I am using python3.
I installed twython on my MAC using pip3 command and I confirmed it was successfully installed.
When I run my python file, it comes up with:
ImportError : No module named twython
My code is as follows:
import sys
import string
import json as simplejson
from twython import Twython
I can't comment the response from #ajxs, but as additional information to his repsonse:
You can change the default python interpreter like this in your MAC terminal:
nano ~/.bash_profile
Add this to the file:
alias python=python3
exit the the bashrc file and run the following command:
source ~/.bash_profile
Now you can check the defaul python version with:
python --version
Maybe this helps you.
First thing that springs to mind is to check that you're running the script with the correct version of Python. Use python --version on the command line to check which version of Python you're executing by default. I've definitely had problems like this before when I've forgotten that my system's default version was 2.7 and I needed to use python3 to run Python 3 on the command line.

Running python script with cmd file results in ImportError

I have a python script named mesh_2.py
import sys
import os
import numpy as np
...
If I run it from Spyder with IPython console it works fine. But I want to run it with a cmd (start.cmd) file. It has the following content:
cmd /c activate py3.4 && mesh_2.py
As I try to run it I get:
ImportError: No module named numpy
I have Anadonda3 installed with a Python 3.4 environment named "py3.4". I am under Windows 7 64 bit.
If I open a cmd.exe myself and write:
activate py3.4
python
import numpy as np
Then everything is all right and I get no error messages. Does any of you have an idea what am I missing here?
Thank you in advance.
try replacing
cmd "/c activate py3.4 && mesh_2.py"
with
cmd "/c activate py3.4 && python mesh_2.py"

Categories

Resources