I am a beginner python learner using The book 'Think Python' where I have to install module name Swampy. The link provided fro instruction and download has a tar.gz file. I found the python 3 version of the swampy with google search here. All setup tools for modules are under python 3. I am pretty lost, how do i install/use the module?
Thanks
You don't have to install Python modules. Just import them. (In fact, swampy is a package, which is basically a collection of modules.)
import swampy
Of course, Python has to know where to import them from. In this case, the simplest thing for you to do is to invoke Python from the directory containing the folder swampy, since the interpreter will first search for modules in the current directory. You can equivalently os.chdir to the directory after invoking Python from anywhere.
Don't worry about setuptools yet.
for python 2.7 I go to C:/Python27/Scripts dir and then try to "easy_install " or "pip install ".
can be a file.
if it does not help: try to unzip downloaded source files and execute "python setup.py install" from command promt
The link is correct but the explanation is vague even for an experienced Windows developer. It assumes too much knowledge of the Python installation process IMO.
e.g. "The simplest way to use this code is to unzip it in your home directory, cd into the unzipped directory and work there."
What is meant by 'home directory'? Then there is a reference to an 'unzipped directory', which I presume means the home directory. The change of name is confusing.
Nevertheless, say one unzips to C:\Python33\lib\swampy-2.1, and works from there. Whatever this means? I can only presume it means save your code in the swampy 'home directory'. It is not best practice to save your python code in a library directory. I use \dev\python\test\ but then
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import swampy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import swampy
ImportError: No module named 'swampy'
However, if the swampy directory is simply renamed to swampy (from swampy-2.1) then all is OK!
>>> import swampy
>>> from swampy.Gui import *
>>> g=Gui()
>>> g.title('Swampy.GUI')
>>> g.mainloop()
FYI this is my path (my dev drive is E: rather C:)
E:\Python33\Lib>path
PATH=E:\Python33\;E:\WINDOWS\system32;E:\WINDOWS;E:\WINDOWS\System32\Wbem;E:\Program Files\Microsoft SQL Server\100\Tool
s\Binn\;E:\Program Files\Microsoft SQL Server\100\DTS\Binn\;E:\WINDOWS\system32\WindowsPowerShell\v1.0;E:\Program Files\
Microsoft\Web Platform Installer\;E:\Program Files\Microchip\xc8\v1.21\bin;E:\Program Files\GtkSharp\2.12\bin
and I don't have a PYTHONPATH environment variable as suggested by other posts.
One easy method is by unpacking the contents into a directory of its own in the Python root directory. Then point Python to the location of the module in the file modulepaths.pth (You may need to create this file in the root directory of your python installation.)
You can either put the full path or a just a relative path. My modulepaths.pth looks like this with one relative and one full path:
swampy
c:\Python34\arduino
Related
I am trying to import a python (2.7.5) module but I'm not sure if I am going at it in the right way. I usually work in Jupyter Notebook (in a seperate Conda env) to keeps things organized per project. Now I am trying to import a module called otbApplication which are Python bindings for a GIS program called Orfeo Toolbox. The thing is, Orfeo Toolbox (together with QGIS) comes with its own Python install (and subsequent paths) and even its own CMD prompt (assuming you use OSGEO4W). If I use this CMD prompt to start Python and import otbApplication, it works fine. But I want to install more packages and just work within my own 'usual' environment (Jupyter Notebook) in this case.
How should you normally reuse modules between Python installations?
I already tried placing a .pth txt file containing the path to the module in one of the sys.path locations of a different Python installation but it wouldn't even find it. I tried to force it by hosting a notebook (with the same Python install) in the same folder as the module and then importing it. I got the following error which makes me question if I am going at this the wrong way:
Python 2.7.5 |Continuum Analytics, Inc.| (default, Jul 1 2013, 12:37:52)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import otbApplication
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "otbApplication.py", line 28, in <module>
_otbApplication = swig_import_helper()
File "otbApplication.py", line 24, in swig_import_helper
_mod = imp.load_module('_otbApplication', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.
This means that forcing the same paths to my new installation is not enough, what am I missing? Apologies for the long story (or the probable butchering of some of the terms).
Kind regards,
Jasper
You've got the right idea with the .pth file, but in order to get it to work you need to check some prerequisites. Obviously, the syntax needs to be good (just give the directory locations separated by line breaks). A common issue is that folks don't put the .pth files in the correct directory (usually though not necessarily \Lib\site-packages). I bet that if you check these you'll be okay.
**Also: as noted in the comments be aware that 32-bit python isn't going to like a 64-bit DLL and verse-visa, so ensure that you're running the right version of python when you try to access those libs.
I have been trying to install the module python-magic for a few hours, and I've been encountering some problems. I am using the 32-bit version of Python 3.5.2 with 64-bit Windows 7.
First, I used the command "pip install python-magic". I downloaded magic1.dll, regex2.dll, and zlib1.dll, and magic from the Files for Windows project, and I copied those four files to
C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info.
I added the aforementioned directory to the beginning of PATH in my Windows environment variables. Then I opened IDLE's Shell and typed "import magic" and got the response OSError: [WinError 126] The specified module could not be found.
I read that the "magic" file should have the extension .dll, so I renamed it. This led to the pop-up warning by Windows "C:\Program Files (x86)\Python35-32\Lib\site-packages\magic.dll is either not designed to run on Windows or contains an error" and by Python, "OSError: [WinError 193] %1 is not a valid Win32 application". I read that the latter error is commonly encountered while running it in a 64-bit environment, but I made sure to run it in 32-bit IDLE and only have the 32-bit version of Python installed.
Following the advice of previous StackOverflow posts, I tried copying cygmagic-1.dll, cygwin1.dll, and cygz.dll to C:\Windows\System32, to the same folder as magic.dll, and I also tried renaming cygmagic-1.dll as magic1.dll, but that didn't have any effect. I know other places say you're not supposed to mix Cygwin Python and Windows Python, but I tried it without the involvement of these files, and it didn't work then, either.
I tried renaming magic.dll to magic.exe, and that allowed "import magic" and magic.Magic(magic_file=r'C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info\magic.exe') with the response "<magic.Magic object at 0x02EA0A70>". When I tried testing with magic.from_file(r'C:\Program Files (x86)\Python35-32\Lib\site-packages\README.txt'), though, I got the error magic.MagicException: b'could not find any magic files! I figured that renaming it to magic.exe had to be wrong, but that it was worth a try.
After I gave up on python-magic, someone recommended an older project. I downloaded it and put the pymagic folder in my site-packages directory. When I tried to import pymagic.pymagic, it told me that the module StringIO doesn't exist, and the recommender told me it was because StringIO is from Python2. I changed all mentions of StringIO to io and tried the command pymagic.pymagic.identify_file(r'E:\Pictures\picture.jpg')
This generated the error TypeError: startswith first arg must be bytes or a tuple of bytes, not str. I'm not involved enough with Python's os, io, etc. modules to know how to make modifications to get this to work. Can anyone make any recommendations on how to get python-magic or pymagic working, or any other module for identifying a file based on its header? I know this question has been asked a lot, but the previous answers didn't work out for me.
Did you call the 'magic' data file magic, and leave it in the same folder as magic1.dll?
Following your instructions I was able to reproduce the same error as you. Using Sysinternals Process Monitor, I could see that the reason for your first error appeared to be that Python was trying to load the the magic data file as if it were the library.
I then renamed the magic data file to magic_data, restarted IDLE, and it worked. I could then use magic to identify a file:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import magic
>>> fn = r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\magic_data'
>>> m = magic.Magic(magic_file=fn)
>>> m.from_file(r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\zlib1.dll')
'PE32 executable for MS Windows (DLL) (console) Intel 80386 32-bit'
(I'm using a different version of Python (3.4), and a different version of Windows (10) to you, but I don't think these matter too much.)
I am currently running Python 3.2 on my computer and need to install Swampy for a book I am reading. Reading many pages and pages on a tutorial has left me further confused. I have downloaded 'swampy1.4'. I am trying to do this by following the set of instructions on this page- http://www.instructables.com/id/How-to-install-Python-packages-on-Windows-7/.
When trying to change directories to simplejson2.6.1 (I have an updated version of this software to the page) I am getting this error- 'The system could not find the specified path.'
Could anyone tell me where I am going wrong? It would be much appreciated.
To get swampy:
Doing a pip install won't install swampy for python 3.
This method should work:
Download the source code from here. Unzip the file to the directory you want. You're going to have to remember this directory.
Next, create a swampy.txt file in the following directory:
C:/Python32/Lib/site-packages
This assumes that you installed python in C:/Python32. You should modify this depending on where you installed python.
Remember the directory in which you unzipped the source code? Type the full path to the source code folder, not the directory you unzipped it into, into swampy.txt. After this, change the extension of the text file you just created from .txt to .pth. What this does is adds the source code to the search path of python.
You should be good to go now.
Quote from Swampy Installation Instructions:
You should see something like this:
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
>>>
If so, you have Python. If the version number starts with 2, you have
Python 2, which is the version of Python used in Think Python. If the
version number starts with 3, you have Python 3. You will not be able
to use Swampy with Python 3.
I suppose you want Swampy just to learn Python. In this case I would recommend you the official Python 3 tutorial.
If you want to continue reading the book don't be afraid of installing Python 2.7.3. Multiple versions of Python can coexist even on Windows assuming that you correctly setup the PATH variable.
There is also a newer version of Swampy (2.1.1).
You can now actually use swampy from source with Python 3.2. Please see http://www.greenteapress.com/thinkpython/swampy/install.html. It clearly says:
Swampy for Python 3 is not available as a package. But the source code is available in a zip file: Swampy source for Python 3: swampy-2.1.python3.zip
I tried to use it under Windows Vista following the instructions on the web page, and at least importing TurtleWorld worked just fine.
The link is correct but the explanation is vague even for an experienced Windows developer. It assumes too much knowledge of the Python installation process IMO.
e.g. "The simplest way to use this code is to unzip it in your home directory, cd into the unzipped directory and work there."
What is meant by 'home directory'? Then there is a reference to an 'unzipped directory', which I presume means the home directory. The change of name is confusing.
Nevertheless, say one unzips to C:\Python33\lib\swampy-2.1, and works from there. Whatever this means? I can only presume it means save your code in the swampy 'home directory'. It is not best practice to save your python code in a library directory. I use \dev\python\test\ but then
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import swampy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import swampy
ImportError: No module named 'swampy'
However, if the swampy directory is simply renamed to swampy (from swampy-2.1) then all is OK!
>>> import swampy
>>> from swampy.Gui import *
>>> g=Gui()
>>> g.title('Swampy.GUI')
>>> g.mainloop()
FYI this is my path (my dev drive is E: rather C:)
E:\Python33\Lib>path
PATH=E:\Python33\;E:\WINDOWS\system32;E:\WINDOWS;E:\WINDOWS\System32\Wbem;E:\Program Files\Microsoft SQL Server\100\Tool
s\Binn\;E:\Program Files\Microsoft SQL Server\100\DTS\Binn\;E:\WINDOWS\system32\WindowsPowerShell\v1.0;E:\Program Files\
Microsoft\Web Platform Installer\;E:\Program Files\Microchip\xc8\v1.21\bin;E:\Program Files\GtkSharp\2.12\bin
and I don't have a PYTHONPATH environment variable as suggested by other posts.
I am teaching myself python from this site. On Chapter 3, when I typed the code in the given example, I got the following error--
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "turtle.py", line 2, in <module>
wn = turtle.Screen()
AttributeError: 'module' object has no attribute 'Screen'
>>>
Is this something that I need to download and install? I tried looking into docs.python.org, but my nose started to bleed reading all that tech stuff.
Kindly point me in the right direction please? Thank you.
Adam Bernier's answer is probably correct. It looks like you have a file called turtle.py that Python is picking up before the one that came with your Python installation.
To track down these problems:
% python
Python 2.7.1 (r271:86832, Jan 29 2011, 13:30:16)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
[...] # Your ${PYTHONPATH}
>>> import turtle
>>> turtle.__file__
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.pyc' # Should be under your Python installation.
>>>
If you see something like this:
>>> import turtle
>>> turtle.__file__
'turtle.py'
Then you'll want to move turtle.py (and any corresponding turtle.pyc or turtle.pyo files) in your current working directory out of the way.
As per the comments below, you'll find a wealth of information about a module, including its pathname and contents by calling help() upon it. For example:
>>> import turtle
>>> help(turtle)
Rename turtle.py. It is clashing with the imported module of the same name.
I tested that the code from that site works in Python 2.6 (without installing any external packages).
From http://docs.python.org/tutorial/modules.html#the-module-search-path
When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH.
So the Python interpreter is finding your turtle.py file, but not seeing a Screen class within that file.
Johnsyweb's answer contains several good tips on how to debug this kind of issue. Perhaps the most direct way of determining where on the filesystem an imported module resides is to use repr(module) or simply type the module name at the REPL prompt, e.g.:
>>> turtle
<module 'turtle' from '/usr/lib/python2.6/lib-tk/turtle.pyc'>
Another problem that people may encounter is due to an installation issue on Linux systems. On my Windows machine, 'turtle' was just there and I was able to import turtle with no problem. When I tried to import turtle in Ubuntu, it didn't find the module, so I tried to install it.
When I did sudo pip install turtle, it installed a package 'turtle' which apparently is very different: "Turtle is an HTTP proxy whose purpose is to throttle connections to specific hostnames ...." This 'turtle' most certainly does not have "Screen" or anything related to a little drawing turtle. So I ended up with the same error as the user in the question of module has no attribute 'Screen'.
For Ubuntu, what I needed to do was:
sudo pip uninstall turtle
sudo apt-get install python-tk
Then when I did import turtle, all of the expected modules were found.
Go to the directory where you save your python files. There is a file named turtle.py. Either remove it, or rename it. This will work.
thanks,
Probably not related, but I spent some time tracking down this same error and found a different cause: I had a file named "copy.py" in the folder with my project.
This "copy.py" was an assignment to make a function that returns a deep copy of a list. The turtle library imports "deepcopy" from "copy"; turns out there's already a "copy.py" as part of python (which I'd never seen/used) & by having a file named "copy.py" in my project folder, it was causing turtle to import the wrong copy.py, which was causing the error to be thrown in turtle (my copy.py assignment's deepcopy function didn't work the same way as the one in python).
This is a more general suggestion, but it's good to double check and make sure you don't have any filenames that are in conflict with actual python imports used in your project. There are too many to list here, but ones used by turtle include: tkinter, types, math, time, inspect, sys, and copy. If you have any of these with a .py in your folder (for example, if you had previously created an inspect.py), turtle will be loading that instead of the built-in library & will not work.
I'm typing at the console
from graphics import *
and I get this error
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from graphics import *
ImportError: No module named graphics
I think your question comes from trying to work through Python Programming by John Zelle, with a Macintosh.
The first 4 chapters are great. But in chapter 5, where I tended to get stuck, he introduces Objects. The way he does that is kind of interesting. He has created a python module which he calls “graphics.py” which you can download from his website.
This module (I’ve attached a link to it below) is a python program written by Mr. Zelle. It creates the tools for making very simple shapes and getting used to the basic concepts with graphics and it also serves as a more tangible way of introducing Objects.
However I was confused, it took me a while to realize that “graphics.py” was a pedagogical program Mr. Zelle created, and not something that comes bundled with MacPython. This confusion stems from the fact that the programs in Chapter 5 all begin with “import graphics” which looks very similar to the “import math” command at the beginning of every program in the 3rd chapter.
The key difference is that “import math” imports the standard mathematics library that came pre-bundled with MacPython. But “import graphics” refers to John Zelle’s own “graphics.py” module, which you have to download and instal first.
It took me a while to figure that out….
Then once I did, I went to his website, copied the program from this website:
http://mcsp.wartburg.edu/zelle/python/graphics.py
...into IDLE and then saved it as graphics.py
This is where it got maddening...
On Windows if you just put the graphics.py file in the same folder as Python, it can find the file and use it without a problem.
Here is what the book said, that made me feel so crazy:
“To use the graphics module (graphics.py) you need to place this file where Python can locate it. >One simple approach is to put it in the same folder where you keep your Python programs. Starting >Python in this folder will also let you import the graphics library to experiment interactively.
Alternatively, you can place the graphics.py file in a system-wide directory so that it is >available for import no matter what directory Python starts in. The standard directory for placing >local additions to Python is the site-packages directory. On my Windows installation, the complete >path to the folder is:
C:\Python23\Lib\site-packages
On my Linux system, the file resides in:
/usr/local/bin/lib/python2.3/site-packages."
On a windows OS, all that you have to do is go to Python.org and download Python for Windows and put that graphics.py file in the main folder, and boom, you’re golden, NOT SO FOR MACINTOSH!!!!
2 years ago, this is where I got totally stuck, because I had no idea about site paths, or directories; I just pointed and clicked; I didn’t know about the Unix system underneath the Macintosh Aqua GUI.
And the book gives no instructions for what to do if you have a Macintosh, and I hit a wall.
But when I went at it again a few weeks ago, those directories made more sense to me, because I spent the spring and summer playing with a guidebook to using the UNIX command shell on my Mac.
So I realized that my problem had nothing to do with Tkinter, It was just that graphics.py needed to be in the right directory. I couldn’t just put it in the folder next to IDLE as I could on a Windows machine. I needed to find the right directory.
Since OSX is built on a UNIX base, I thought that the file path might be the same as Linux. Sadly, there was no “local”, “bin” after the “user” directory. But its not quite the same in OSX.
Instead you can use IDLE itself to find out which path directories it uses.
You type this:
import sys
print sys.path
and BOOM, it spits out a whole bunch of directories, confusingly formatted like this
['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages’,
'/Library/Python/2.7/site-packages’]
But when I looked closer I noticed the directory “site-packages” which looked a lot like the “site-packages” on the Linux and Windows command lines.
So I pulled out that directory chain
['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload’,
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', ‘
/Library/Python/2.7/site-packages’]
Then using the UNIX shell I followed it down, and then I did the same thing in the Aqua interface.
And there, buried 8 levels down in the directory, amidst hundreds of other files, I placed my graphics.py file.
Then I went back into IDLE, typed in “import graphics”
AND IT WORKED!!
If you are having this problem. I hope this solution saves you my headache.
did you follow the instructions to install the graphics module and is it in your pythonpath?
Type how to install graphics.py in Google.
http://www.google.co.in/search?sourceid=chrome&ie=UTF-8&q=how+tooinstall+gaphics.py#sclient=psy-ab&hl=en&source=hp&q=how+to+install+gaphics.py&pbx=1&oq=how+to+install+gaphics.py&aq=f&aqi=&aql=&gs_sm=s&gs_upl=0l0l0l24645l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=344adf6de4d60025&biw=1366&bih=600
Click on the third link
http://mcsp.wartburg.edu/zelle/python/graphics.py
Copy that file which got downloaded to your lib folder in Python32 in C drive.
The error will not occur now.
Short cut to installing graphics.py via command line and/or terminal window. run the following command in admin rights in windows.
this will allow you to import graphics
pip3 install --user http://bit.ly/csc161graphics
ps. Im running python 3.6.2 on Atom. Hope this helps some.
Perhaps you forgot to run python setup.py -install after downloading the module.
You can also put graphics.py in the same folder where you started the python shell.
http://mcsp.wartburg.edu/zelle/python/graphics.py
I recently had this issue where I typed (and pasting the code into the interpreter did not work either) 'import graphics' into the interpreter and the error I got was:
Traceback (most recent call last):
File "", line 1, in
from graphics import *
ImportError: No module named graphics
I took the following steps to fix the error:
-Go to: http://mcsp.wartburg.edu/zelle/python/
-Right click on the link 'graphics.py and click 'save target as'
-save in the directory 'Python33', along with the 'chaos.py' file. This step is important that you do not save it in other locations (it may work if you save it in other locations I decided to save it in this location).
Hope this helps.