Running python script with cmd file results in ImportError - python

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"

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.

Mac Terminal ttys000 back to Console (base)

I am a novice coder and I am currently learning python and the Mac terminal.
Main Question: How do I return back to the original state of the Mac Terminal where it says Console and (base)? My mac terminal is currently on ttys000. (Both are -bash)
Originally when opening the Mac Terminal, I was presented with this:
Last login: Sat Jan 5 13:30:29 on console
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) Myusernamehere-MacBook-Pro:~ Myusernamehere$
However, currently, when opening the Mac Terminal, I am presented with this:
Last login: Sat Jan 8 19:18:51 on ttys000
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Myusernamehere-MacBook-Pro:~ Myusernamehere$
Why is this occurring?: I was attempting to make my python code become an application via py2app. This did not work so I tried converting the python script into a DMG file. Here are some of the tutorials I attempted to follow:
https://www.youtube.com/watch?v=kSw4_lMqR3M
https://www.youtube.com/watchv=DVOoHL2Bp_o&list=PL5RJRIWAZ174f_lkWRkjk3nFCEZs_VBFa&index=13&t=198s
https://www.youtube.com/watch?v=IIAlkQEw8Gc
Why I am looking to get this fixed?: When the Mac terminal was in its original state with Console and (base), it recognized my pip imports for PIL, mplfinance, pandas_datareader, and yahoo_fin. Now when the mac is on ttys000, PIL, mplfinance, pandas_datareader, and yahoo_fin are no longer recognized as module names. In the mac terminal, after attempting to run the program (the name of my vscode python script is called V1_terminal, I am presented with this:
Myusernamehere-MacBook-Pro:Stock_Market Myusernamehere$ python V1_terminal.py
Traceback (most recent call last):
File "V1_terminal.py", line 4, in <module>
from PIL import ImageTk, Image
ImportError: No module named PIL
My program is stored in vscode with python 3.8.8 64-bit ('base': conda) Thus, I am unable to run the code.
In vscode, in python, this is my import lines of code:
#imports for GUI
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
import os
from numpy import loads, place
# for Yahoo Finance
from yahoo_fin.stock_info import *
import yahoo_fin.stock_info as yaf
import mplfinance as mpf
# for data series and plot
from pandas_datareader import data
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import requests
import matplotlib.pyplot as plt
Thank you so much in advance for offering advice and helping me! If I should add more context please let me know. Thank you!
Updates:
I tried conda activate in the terminal window and this is the result:
Last login: Mon Jan 10 13:13:22 on ttys000
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Myusernamehere-MacBook-Pro:~ Myusernamehere$ conda activate
-bash: /opt/anaconda3/bin/conda: /opt/anaconda3/bin/python: bad interpreter: No such file or directory
Myusernamehere-MacBook-Pro:~ Myusernamehere$
I tried conda config --set auto_activate_base true and this is the result:
Last login: Mon Jan 10 16:11:40 on ttys000
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Myusernamehere-MacBook-Pro:~ Myusernamehere$ conda config --set auto_activate_base true
-bash: /opt/anaconda3/bin/conda: /opt/anaconda3/bin/python: bad interpreter: No such file or directory
Myusernamehere-MacBook-Pro:~ Myusernamehere$
So what I ended up doing is deleting Anaconda-Navigator from my Macbook then attempted to redownload it but ran into the error:
Anaconda3 is already installed in /opt/anaconda3. Use 'conda update anaconda3' to update Anaconda3.
So I used the answer found in this StackOverflow post and punched it into the Mac Terminal and successfully redownloaded the Anaconda-Navigator:
Anaconda-Navigator Error
I reinstalled mplfinance, yahoo_fin, etc via pip in the Mac Terminal and my python code successfully runs! The Terminal is still on ttys000 (and not Console like originally) but I am successfully running code. Feels like a rudimentary way to solve this but seems like everything is okay now. Thank you to you all for your time helping me!
here is what you can do:
Setup a virtual env
python3 -m venv venv
source venv/bin/activate
pip install tk numpy yahoo_fin mplfinance pandas pandas_datareader requests matplotlib
Optionally save the requirements into a file.. "pip freeze > requirements.txt" and check it in with your code. Next time you can do "pip install -r requirements.txt"
Now run your program.
Setting up a venv is a one time thing.. next time you open a new terminal, just activate the venv
source venv/bin/activate
you now are inside an environment which has your packages installed..

Mac OS VSCode built-in terminal can't import installed python packages even the corrected conda environment selected

Mac OS VSCode built-in terminal can't import installed python packages.
I've installed NumPy correctly using Anaconda in one of my environments called tf20;
And then I created a python script scene.py
import numpy as np
print(np.__version__)
I can run this script in my system Terminal like:
(tf20) mac#Macbook:~/Desktop/manim_project$ python scene.py
1.18.5
However, an import error occurred when I run the same script.
(tf20) mac#Macbook:~/Desktop/manim_project$ python scene.py
Traceback (most recent call last):
File "scene.py", line 10, in <module>
import numpy as np
ImportError: No module named numpy
However, when I run this by pressing F5, the debug mode, the problem disappeared.
(tf20) mac#Macbook:~/Desktop/manim_project$ cd /Users/mac/Desktop/manim_project ; /usr/bin/env /Users/mac/.jackprogram/anaconda3/envs/tf20/bin/python /Users/mac/.vscode/extensions/ms-python.python-2021.11.1422169775/pythonFiles/lib/python/debugpy/launcher 62372 -- /Users/mac/Desktop/manim_project/scene.py
1.18.5
(tf20) mac#Macbook:~/Desktop/manim_project$
I think I selected the right python environment tf20 in VSCode because
the VSCode built-in terminal showed (tf20) at the beginning of the terminal prompt.
the left corner of VSCode showed Python3.7.7 64-bit ('tf20:conda').
and I even used cmd + shift + p then Python: Select Intereter to ensure I selected the right python environment.
Because of this problem, I have to use the macOS terminal application to run the Python scripts for a half year.
Help. Please!

bash: Python import - Command not found for pandas

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"

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.

Categories

Resources