By accident I noticed that both csv and re modules of python standard library have their .__version__ attribute:
>>> import re, csv
>>> re.__version__
'2.2.1'
>>> csv.__version__
'1.0'
It surprises me as they are part of the standard library, so I would expect their version to be defined by sys.version (and sys.version_info).
I have noticed the values of the attributes are same for both Python 2.7.13 and 3.6.1, despite the modules have changed.
Are they just a kind of "code fossils" or are they somehow meaningful and programmers should pay attention to their values?
I can assume that the source version of the module on C did not change, only the source code of the python module has changed across different versions of python itself. Looking for source code in python repository can shed light into whole situation.
For example:
CSV C source code
CSV Python source code
Related
Have a look at the error in the attached scrren shot.
new is the directory which contains unittest module of python 3.6 and new2 contains unittest module of python 2.7. I understand the error raised in first case is because of missing StringIO module. But why python 2.7 is raising error despite writing the same command as in python 3. Is the syntax different in the two cases?
Does this help?:
import sys
sys.path.append('path/to/your/file')
import your.lib
You need an __init.__py file. See here: https://docs.python.org/2/tutorial/modules.html#packages
The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.
I recently installed Python 3 on my Mac OSX 10.6.8 and I haven't had any problems with modules or imports until now. I'm writing a function that tests whether or not a triangle is right angled based on the length of the sides and the guide that the exercise was in has a bunch of equalities to test so I can see if it works:
testEqual(is_rightangled(1.5,2.0,2.5), True)
testEqual(is_rightangled(4.0,8.0,16.0), False)
testEqual(is_rightangled(4.1,8.2,9.1678787077), True)
testEqual(is_rightangled(4.1,8.2,9.16787), True)
testEqual(is_rightangled(4.1,8.2,9.168), False)
testEqual(is_rightangled(0.5,0.4,0.64031), True)
I should apparently import a function called testEqual(a,b,c) from a module called test, since the example programme in the guide starts with from test import testEqual, but when I typed that into my file I got this message:
from test import testEqual
ImportError: cannot import name testEqual
I suppose I should specify the path to the test module, but I can't find it my Python 3 library anywhere in my computer – just the 2.x ones that came installed with the computer, which are in /Library/Python. import turtle and import math worked, so it must be somewhere.
The test module in the Python stdlib doesn't contain a function called testEqual(). Its documentation starts with
Note: The test package is meant for internal use by Python only. It is
documented for the benefit of the core developers of Python. Any use
of this package outside of Python’s standard library is discouraged as
code mentioned here can change or be removed without notice between
releases of Python.
Are you sure that this guide you're following doesn't have its own test.py program that you're supposed to use instead?
When you write your testEqual() function make note of the directory you are working in. For instance on my mac I created a directory (folder) in documents so my path looks like: /Users/myName/Documents/python. Save your function (module) as testEqual.py and when you write you test.py script import testEqual after the shebang line. Once you have your scripts debugged your modules will be in a folder that python creates titled pycache don't remove that as it is compiled code. Now, as long as you are working in the same dir as your module you should not need to do anything other than use the import statement.
I'm trying to understand if it is simply impossible to load a python module from a unicode path, or if there is some trick I am missing.
This bug report seems to imply that it is not possible:
http://bugs.python.org/issue11619
Goal:
suppose C:\Users\pkarasev\д contains Foo.py , then I want to do this:
import sys
sys.path.append(str('c:/Users/pkarasev/\xd0\xb3').decode('utf-8') )
from Foo import *
This fails with "cannot find module..." although u'c:/Users/pkarasev/\0433' has been added to my sys.path and 0433 is the correct encoding for д.
note that the str(...).decode(...) method works for things like os.open, but for some reason not for loading modules. Is there a different format for the encoding? Is this action impossible, period? Do I need to use python 3.x instead of 2.7.3 with some different syntax?
edit: cash award is eligible if someone knows a trick to do this (on windows)
Yeah it is either a bug in python for not supporting windows, or a bug in windows for not being sane and using utf-8 encoding. In python27.dll you can step in and see the bogus module paths...
So I have a python script calling some other pythons scripts in the working directory. I usually use naming conventions like v1.0.3_ModuleName.py to keep track of newer versus older versions of my script. When I tried to import my module:
import v1.0.3_ModuleName
I recieved the good ole: SyntaxError: invalid syntax error. Now I realized my error quickly and took out the periods.
This make me wonder, what other file names with result in errors when you try to import them into python?
If you're using linux you could make a symbolic link to your module that doesn't include dots and numbers :) But this isn't a portable solution.
More about module naming.
I've got a copy of Lion with all the dev tools installed. Both Python (2.7) and Ruby (1.8) are running just fine. I've installed the Natural Language Tool Kit for Python and tried it out in the Python interpreter and it works
import nltk
>>true
So that works. I've also installed the RubyPython gem, and it seems to work too, but it can't find the nltk module. It's possible I'm doing something wrong. In irb:
require "RubyGems"
require "rubypython" #both true
RubyPython.start # true
n = RubyPython.import "nltk" # RubyPython::PythonError: ImportError: No module named nltk
c = RubyPython.import "cPickle" # works!
RubyPython.stop
I can't figure this one out. The PythonError seems to indicate to me (because it's just a call to the direct C APIs) that the nltk module can't be found by any form of python. But the interpreter finds it just fine. RubyPython, however, cannot.
I've also tried forcing RubyPython to use python2.7 but no change.
What am I missing?
It's simple! For some reason, RubyPython was looking in the wrong place for my Python modules. This was verified by importing sys in both the RubyPython script and in Python, and comparing sys.path. I ended up fixing it by taking the path list of sys in the Ruby script and adding what was missing from the pure Python's path. Then I could load NLTK.