I have written a small python module called phpcs. I
folder structure
cs_qa_support
phpcs
init.py
config_file.py
setup.py
contents of setup.py
from setuptools import setup
setup(name='cs_qa_support',
version='0.1',
description='QA Support',
packages=['phpcs'],
zip_safe=False)
I installed this using the command
pip install -e .
the file phpcs/init.py is empty
the content of phpcs/confif_file is
class PhpcsConfigFile:
def __init__(self,configFileName):
self.__config_file_name = configFileName
def dump(self):
print self.__config_file_name
given all of that I want to start using the module
my first attempt
>>> import phpcs
>>> config_file.PhpcsConfigFile
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'config_file' is not defined
second attempt
>>> from phpcs import config_file
>>> config_file.PhpcsConfigFile
<class phpcs.config_file.PhpcsConfigFile at 0x7fe5ef4b7738>
My question is can I avoid typing config_file.PhpcsConfigFile and just use PhpcsConfigFile?
Thanks
you can do it like this
from phpcs import config file as con
then do this
con.phpcsConfifFile
Related
I have the following project:
apack/
apack/
__init__.py
apack.py
aux.py
setup.py
apack.py:
def foo(x):
return x
aux.py:
from apack import foo
def bar():
return foo() + 1
setup.py:
from setuptools import setup, find_packages
setup(
name='apack',
packages=find_packages()
)
I install the package with pip install .. However, the result is a an empty package:
>>> import apack
>>> apack.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'apack' has no attribute 'foo'
What I would like to have is:
>>> import apack
>>> apack.foo()
>>> apack.bar()
There is a number of similar questions here (e.g., 1, 2). However, none of them is minimal enough for me to understand why my example does not work.
When you import the package it invokes the __init__ file of the package. You can confirm this in your python interpreter.
>>> import json
>>> json.__file__
'<PATH>\\lib\\json\\__init__.py'
So to have foo and bar available from your package level you have to import foo and bar into the __init__ file of your package. ie,
from .apack import foo
from .aux import bar
I am trying to create a module using setuptools. I installed setuptools and wrote a small piece of code as TestFunction.py:
def function(test):
print(test);
function("hello World")
and created a setup.py file with the below instruction:
from setuptools import setup
setup(
name='TestFunction.py',
version='1.0',
description='this is a python distribution',
py_module=['TestFunction'],)
Now i am running python3 setup.py sdist and getting the below error. My os is ubuntu 18. Much appreciated.
Original exception was:
Traceback (most recent call last):
File "setup.py", line 1, in <module>
from setuptools import setup, find_packages
File "/home/jeet/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 6, in <module>
import distutils.core
File "/usr/lib/python3.6/distutils/core.py", line 16, in <module>
from distutils.dist import Distribution
File "/usr/lib/python3.6/distutils/dist.py", line 18, in <module>
from distutils.fancy_getopt import FancyGetopt, translate_longopt
File "/usr/lib/python3.6/distutils/fancy_getopt.py", line 373, in <module>
WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}
AttributeError: module 'string' has no attribute 'whitespace'
It seems you have a file string.py that isn't a module from the standard library but your own module or script. Rename it so it doesn't overshadow the standard module; don't forget to remove string.pyc file; for python 3.6 it's in __pycache__ directory.
I changed your code because you have an indentation formatting error. Rest of your code works fine.
copy and try this:
test_module.py
def function1(test):
print(test);
setup.py
from setuptools import setup
setup(
name='test_module.py',
version='1.0',
description='this is a python distribution',
py_module=['test_module'],)
usage:
python setup.py install
use your function:
from test_module import function1
function1("hello stackoverflow")
output:
hello stackoverflow
I am trying to import the importlib module but I get this message:
>>> importlib
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'importlib' is not defined
I have manually located the importlib file within the Python folder, so I know I have it. I am running Python version 3.4.1. I also tried to pip install importlib but it wasn't found either.
What's going on?
You need to use an import statement to load it in:
>>> import importlib
>>> importlib
<module 'importlib' from 'C:\\Python33\\lib\\importlib\\__init__.py'>
I tried to import a file var.py to openfile.py. var is passed as an argument to openfile. I am unable to figure out how to import it by reading the name from command line.
var = sys.argv[1]
importlib.import_module(var, package=None)
I tried the above code but it gave the below error
Traceback (most recent call last):
File "true.py", line 7, in <module>
importlib.import_module(var, package=None)
NameError: name 'importlib' is not defined
you are missing importlib module.
Thats why you are getting NameError: name 'importlib' is not defined
do import importlib
Note:
If imporlib is not installed, you can install it using easy_install/pip on *nix:
easy_install importlib
You have to import the importlib module first:
import importlib
....
importlib.import_module(...)
I have installed libsvm-3.12 and placed into:
/home/ubuntu/libsvm-3.12
In my pythonpath I have the following:
echo $PYTHONPATH
/home/ubuntu/libsvm-3.12/python:/home/ubuntu/libsvm-3.12:$PYTHONPATH
This is in both .bashrc in home and in /etc/enviroments
I rebooted the machine.
From python I get:
>>> import svmulti
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named svmulti
How do I let python know where the lib is?
Given installation via make in .../libsvm-3.12/python,I think you also want:
>>> import svmutil
not:
>>> import svmulti