python3 - ImportError: No module named twython - python

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.

Related

VS Code is not recognizing 'nmap'

I am trying to make a nmap scanner for the InfoSec Certification on freeCodeCamp.org and cannot get Visual Studio Code to recognize that I have installed nmap. I am very beginner and in the process of learning.
from cProfile import run
import nmap
scanner = nmap.PortScanner()
print("Welcome, this is a simple automattion tool")
print("<------------------------------------------->")
When I run this in VS Code I get the following in the terminal:
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1> python3 scanner.py
Traceback (most recent call last):
File "C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1\scanner.py", line 2, in <module>
import nmap
ModuleNotFoundError: No module named 'nmap'
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1>
I have so far:
Updated to the current Python 3.10.7
Installed Nmap the first time from https://nmap.org/ for Windows
Uninstalled Nmap
Reinsalled Nmap using >>>pip3 install python-nmap
For future searchers (as in my comment above), if you installed a module with pip3 but are still getting module import errors, try python3 -m pip install <module-name>.
Not sure how/why this happens (pip3 installing somewhere that python3 is not looking for modules - maybe PYTHONPATH-related), but the above can [usually] help.
It seems that Python cannot find nmap module.
You can either add it to path in Windows via Start -> Edit system environment variables -> Environment variables... and then edit PATH VARIABLE by adding at the end path to nmap module that you have installed or move the module to default Python modules directory which should be C:\Users\YourLogin\AppData\Local\Programs\Python\Python310\Lib\site-packages

Run Python module in another code but with an older version

I am using Python 3.10 but I want to run a Python module with Python 3.6 and use the result inside my code(that written with Python 3.10)
Please help me to solve it
thanks
Create a .py script that should be run with python3.6
Make the script prints your expected result
# script.py
import sys
print(sys.version[:6])
Use subprocess to get it
import subprocess
python3_6_result = subprocess.check_output(["python3.6", "script.py"])
print(python3_6_result) # 3.6.15

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..

I have a problem when I try to run python command in bash

I'm trying to run this command in bash: python3 HelloServer.py (HelloServer.py is the python file I want to run). It returns
["bash: python3: command not found"][1]
and if i run the following command...
python HelloServer.py it returns
"Traceback (most recent call last):
File "HelloServer.py", line 7, in <module>
from http.server import HTTPServer, BaseHTTPRequestHandler
ImportError: No module named http.server"
this is the bash:
this is the HelloServer.py code
Based on that module, it looks like the file you're trying to run is meant for python3 which you don't seem to have installed. Easily remedied though: https://www.python.org/downloads/
There are two possibilities, first is that as indicated by Bernard you don't have installed python 3, the second is that you do have python 3 installed but you are using python 2.7
It seems you don't have python3 installed. Try which python3 in your shell (Linux). The http.server module is for python3 only. If you want to stick to python2, SimpleHTTPServer might be what you want.
I seems that you haven't python3 on your system.
You need to install python3 first.
apt-get install pyhton3
After this you could write command in python3

Python: installing multiprocessing

I need to import the multiprocessing module in Python 2.5.
I've followed the instructions here exactly: http://code.google.com/p/python-multiprocessing/wiki/Install
make and make test run without errors. I've also edited $PYTHONPATH to include the directory where the package is installed.
But 'import multiprocessing' still says: "ImportError: no module named multiprocessing".
What am I doing wrong? Is there some step missing from these instructions? I haven't installed a Python module before.
Navigate to the directory containing the package then type:
python setup.py install
This info was contained in the INSTALL.txt file.
http://code.google.com/p/python-multiprocessing/source/browse/trunk/INSTALL.txt
perhaps you can try:
import sys
sys.path.append('/path/to/processingdotpylibs/')
import processing

Categories

Resources