unable to import module pyautogui - python

I am working on OS X
Ive looked at many different threads and haven't found a solution. Maybe I haven't been installing things properly from the start either. I want to import the module pyautogui into my project but it is saying it can't find it. I did a pip install in terminal and in the pycharmterminal as well. I have found the file directory and verified that it is available. But yet I am still not able to access it in my project. On some of the threads I read something about init.py, do I need to put that in my project. If so is there code in that py file or do I just create a python file named init.py

On Mac OS and some Linux distros, you may need to type pip3 install package-name.

Related

Python: No Module named xxx

Im getting nowhere with the following error on my Raspberry Pi:
My own Python script calls a function from another module named BlackBean.py which in turn imports other modules called "netaddr" and "configparser". The problem is that I just cant seem to get past the import error which tells me " No Module named netaddr, or if I comment out that import then it also errors with No Module named configparser. So I know its a path issue but I just cant seem to get it fixed!
The Blackbean.Py script starts like this:
import broadlink
import ConfigParser
import sys, getopt
import time, binascii
import netaddr
import BlackBeanSettings
import re
from os import path
from Crypto.Cipher import AES
SettingsFile = ConfigParser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(BlackBeanSettings.BlackBeanControlSettings)
def execute_command(etc.........
The BlackBean.py file is in my project SkyHD folder at /home/pi/SkyHD.
The "netaddr" and "configparser" files & folders were installed by pip in /home/pi/.local/lib/python2.7(and python3.5)/site-package folders.
sys.path has the above folders in its list and Ive also edited .bashrc and added PYTHONPATH=${PYTHONPATH}:/home/pi/.local/lib/python2.7/site-package:/home/pi/.local/lib/python3.5/site-package:/home/pi/SkyHD:../
but none of this works. I guess it must be something basic but I just cant work it out! help!
Also, some more info, when I first install all the files and run my program everything works fine and it finds the files ok with no problems, its only when I reboot it fails to find the files.
Its fixed.
Python looks for imported modules in 3 places, the first being the folder you launched the python script from; so for me the obvious answer is to import the modules I need directly into my own Project folder (/home/pi/myproject). This worked just fine, it works every time even after reboot, which was my main problem before. No need to create or alter PYTHONPATH, no need to mess around with entries in .bashrc or try to change the python path entries. Here are the steps:
Upgrade PIP to version 9.0.3 (not ver 10) with
pip install --upgrade pip==9.0.3
then install the required modules with the following
pip install --target=/home/pi/your_project_folder module_name
so for me it was... pip install --target=/home/pi/SkyHD netaddr
Im sure this is not best practice, but my Raspberry Pi only has this one project to run and having modules imported into the Projects folder just isnt an issue.
Hope this helps some others with the same problem.
You've provided insufficient information. Specifically, details about the python command being used to run your script such as its version (python -V) and its module search path if you do
env -u PYTHONPATH python -c 'import sys; print(sys.path);'
Similarly you can easily simplify the problem. What happens if you do python -m netaddr?
Obviously in the above commands substitute the actual python command being used to run your script.
And, as #BoarGules mentioned in his comments to your question, you should never, ever add directories to PYTHONPATH for different python versions unless you know that the modules in those directories has been written to work with python2 and python3.

How to install a module on Python?

Okay, so, I'm actually a beginner in programming Python, and I only found out yesterday how you were supposed to encode pip install ModuleName in the Python command line and not in the interactive shell. I'm trying to download a lot of modules, such as the Send2Trash module, Pyperclip, Requests, Beautiful Soup, and Selenium.
Before I checked the forums about installing modules, I found out how we needed to have the pip tool. I'm a Windows user, but for some reason, I didn't have the 'Scripts' folder installed when I downloaded Python. I didn't know we needed it, so I used raw scripts from GitHub, setup.py, and copy pasted the script into the File Editor in Python, ran it in the interactive shell, and tried to import the module I needed. It worked for the Pyperclip and the Requests module; no errors popped up after I imported them using import pyperclip or import requests, but when I tried the same procedure for the rest of the modules I needed, there were some errors.
Also, when I tried to download the modules on pypi.python.org, I tried to open it using the interactive shell, but then something pops up, 'The file's encoding is invalid for Python3.x...', and when I click 'OK', it's going to say 'Failed to Decode', and close everything.
So, after reading forum after forum, I found out how to download pip, and was also able to download setuptools and wheel. I'm not sure if it's really already downloaded, but I was able to get the 'Scripts' folder that wasn't there before, so I guess so. I also already went into my PATH using the edit environment for your account thing, and I edited the Path variable so its value would lead to my 'Scripts' folder. Please do tell me if I did the right thing here.
So, following the advice of the forums, I tried to install the modules I needed by typing pip install ModuleName in the Python command line instead of the interactive shell, but it still gave me a Syntax Error. I also tried it in Command Prompt, typing the same code pip install ModuleName, but when I clicked Enter, nothing happens; no errors or anything. It seemed like my install was accepted, but when I tried importing the module in the interactive shell, it still gave an Import Error.
Please tell me what I did wrong throughout my process, and how to properly install the modules I need. I would include pictures into this, but it seems I can only add two before my reputation becomes 10, and I'm pretty new here, so... If there's anything I need to elaborate on about my problem, don't hesitate to ask, and I'll try my best.
You say you use windows so you need to understand pip.
pip is a program that installs python modules. You can even use easy_install instead of pip.
some pip commands
pip list -- lists out already installed modules.
pip search <module name> -- searches new modules.
pip -h -- more pip commands you want.
pip installs modules from CMD prompt not from python shell.
Even after installing modules some modules doesn't run as import module
they need to be imported as from module import function.
refer the pip help command and install modules.
DO NOT SAVE SCRIPT FILES IN PYTHON ROOT FOLDER YOU MAY FACE SOME PROBLEMS
Happy Programming!!!
After a whole lot of searching and trying out, I found the solution to my problem. For future Python users who encounter the same thing: always install your modules in the root folder.
In my case, my Command Prompt was automatically inside the C:\Users folder, which caused some problems because I couldn't download my module in there. Once I typed in cd C:\Python34, which was my root folder, I could successfully download the modules I needed using pip install ModuleName.

ImportError: No module named twisted.persisted.styles

From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.
I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).
From the import of scrapy on the first line, I get this error when running the program:
ImportError: No module named twisted.persisted.styles
The whole script, if it's helpful, points to this:
Traceback (most recent call last):
File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
import scrapy
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ImportError: No module named twisted.persisted.styles
Any suggestions on how to tackle this problem?
Just try to force the update of twisted :
pip install twisted --upgrade
That works for me with python3.4 and Scrapy==1.1.0rc1
Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:
The easiest way to install a python package is through pip.
If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.
note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!
It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:
Installing Twisted through pip broken on one server
Had this exact thing on FreeBSD. Solution (as root/sudo):
chmod -R go+rX /usr/local/lib/python2.7/site-packages
Some directory permissions weren't set up right on install.

How to install apscheduler

I would like to use scheduler in my python program however I haven't been able to install it.
I tried with Easy_Install and PIP (neither of which I've used before) and I can't find a link for another method. I'm using Python 2.7 on Windows Vista
Since I've never used PIP before I had to install that first. After installing pip I went to command prompt, changed to the directory with pip and typed:
C:\Python27\Scripts>pip install apscheduler
It didn't come up with an error so I assumed it installed, however when I run my python program, which includes the line: from apscheduler.scheduler import Scheduler
it states:
ImportError: No module named apscheduler.scheduler
and when I look at the list of installed modules in Idle it's not there.
It's probably something obvious since I don't have a lot of experience in programming yet.
Help would be much appreciated!
sm
Hi again,
I got it working finally, in the end I didn't use PIP, in case other people need help this is what I did:
Downloaded the apscheduler tar.gz file
Downloaded 7-zip, since this can extract tar.gz files on Windows.
Extracted the tar.gz file using 7-zip, I had to do this twice since the first time I clicked extract it extracted to a .tar file (APScheduler-2.1.2.tar), it was necessary to extract this file as well.
Added C:python27\ to the windows path (this is in control panel->system & maintenance->system->advanced system settings->environment variables)
(I also added C:python27\scripts\ to the path, but not sure whether this makes a difference.
Opened command prompt and moved to the folder containing the extracted APScheduler files including the file named setup.py
In my case this was- C:\Python27\APScheduler\APScheduler-2.1.2\APScheduler-2.1.2\
In command prompt typed> python setup.py install
Hopefully this was everything, perhaps one day I'll delete everything and try again to check, but it took quite sometime to get it going so right now I think I'll leave it as is.

How to get _sqlite3.so file?

I have installed Python 2.6.2.. I did it "locally" since I do not have root permissions. With this version of Python I wanted to use module called "sqlite3" (it is called "pysqlite" in earlier versions). In theory I had to be able to use this module without any problems since it is supposed to be included by default in the considered version of Python. However, I have some troubles. When I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "", line 1, in File "/home/verrtex/opt/lib/python2.6/sqlite3/init.py", line 24, in
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ImportError: No module named _sqlite3
As I have already told to, the possible reason of this problem is that the module in tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. This explanations is supported by the fact that I do not have _sqlite3.so file in my "/home/verrtex/opt/lib/python2.6/lib-dynload" directory. So, this is the problem I have to solve (I have to get this file to this directory).
I found out that to solve this problem I have to "install sqlite3 and recompile Python". I also found out that the problem can be solved by "building from source and moving the library to /usr/lib/python2.5/lib-dynload/".
However, it is not clear to me what exactly should I do. Should I install python module called "sqlite3" or I should install sqlite-database? By the way, I have already sqlite-database installed globally by the administrator. Can I use it or I still have to install my own database? By the way, I do not have root permissions. Can it be a problem? Or I need to install a python module? Is absence of root permissions a problem, in this case?
I also has been told to, to take source files from SQLite Download Page, extract archive, move to expanded directory and execute:
./configure
make
make install
Then I have to copy newly compiled files to my Python directory. Should I copy all newly compiled files? And to which exactly directory should I copy (my Python directory have some subdirectories)?
Would very appreciate any help, because I stack with this problem for a wile.
P.S. My OS is CentOS release 5.3 (Final).
Your sys.path is likely not pointing to your locally installed copy, or you're not running the Python 2.6.2 you think you are.
If none of that is the case, you need the SQLite development headers (sqlite-dev or whatever), and then recompile Python. You need to pay attention at the end of the compile, because it complains about what it didn't build due to missing dependencies.
EDIT: Reread question.
EDIT 2: Also, please don't do this:
from module import *
Do this:
from module import what_i_need
import module2
Although you might have found your solution, I just wrote mine down for someone who are stuck in the same problem.
My OS is CentOS 6.3(Final) with python2.6.
I install python2.7.3 in my system, but the problem's still there. (_sqlite3.so should be in /path/to/python2.7.3/lib/python2.7/lib-dynload after python2.7.3 has been installed. Because before python2.7 was installed, sqlite-autoconf-3071502.tar.gz was installed.)
I then copy the /path/to/python2.6/lib/python2.6/lib-dynload/_sqlite3.so to the python2.7's path. And type in the python-shell:
>>> import sqlite3
or
>>> import _sqlite3
No error reports.
Unfortunately, the damn error appeared as before when I run my python script.
I install sqlite-devel(sudo yum install sqlite-devel or download here), and then reinstall python2.7.3 again. Run my python script again. Thank goodness! The damn error finally solved.

Categories