how to import file passed as command line argument in python - python

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

Related

Downloading Modules on Python

Im looking to download modules on Python but I seem to be running into some problems. Whenever I import a new module, an error always shows up. Here is an example of the code that I am using to download a module:
import openpyxl
and this is the error that shows up when I try to run this code:
Traceback (most recent call last):
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/automation.py", line 1, in <module>
import openpyxl
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/__init__.py", line 4, in <module>
from openpyxl.compat.numbers import NUMPY, PANDAS
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/compat/__init__.py", line 4, in <module>
from .strings import safe_string
File "/Users/jonathankent/PycharmProjects/untitled/HelloWorld/venv/lib/python3.8/site-packages/openpyxl/compat/strings.py", line 4, in <module>
from math import isnan, isinf
ImportError: cannot import name 'isnan' from 'math' (/Users/jonathankent/PycharmProjects/untitled/HelloWorld/math.py)
Process finished with exit code 1
If anyone knows what could be causing this please let me know.
ImportError: cannot import name 'isnan' from 'math' (/Users/jonathankent/PycharmProjects/untitled/HelloWorld/math.py)
It seems you have named your own module math. This shadows the built-in math module.
The error means that some other code wanted to import the built-in math module but found your module instead.
The simplest solution would be if you named your module differently.

importing my own python modules

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

Python module not callable when importing getopt

I am new to python and the getopt function. I am trying to import getopt, however I run into an error.
My code is literally just:
import getopt
and also tried
from getopt import * /// from getopt import getopt
Output below:
python asdfasdf.py
ARGV : []
Traceback (most recent call last):
File "asdfasdf.py", line 1, in <module>
import getopt
File "/home/STUDENTS/~~/csc328/TCP/pyExample/getopt.py", line 12, in <module>
'version=',
TypeError: 'module' object is not callable
My python is 2.6 something and this was implemented in 2.3 I believe.
edit: I'm using unix
you might be have getotp module in your code. When you refere python third party module, it try to import your module.
Pleas remove getopt.py in your example or rename it.

Python Can't Find Importlib

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

How to mock python module in unittest

I have a problem when mocking in unittest.
#!/usr/bin/env python
import sys
sys.modules["foo.Bar"] = __import__("mock_bar")
import foo.Bar
print foo.Bar.__name__
I've got an ImportError exception in line 4. I don't know why since I have do some mock at line 3. There is a reference of how to mock import here.
Here's the error message:
Traceback (most recent call last):
File "test.py", line 4, in <module>
import foo.Bar
ImportError: No module named foo.Bar
"import foo.Bar" should equal to "__import__('foo.Bar')", and before that I've hacked sys.modules to pretend module 'foo.Bar' has been already imported. Why python still try to import foo.Bar and complain?
Try doing import foo before your __import__ line: I think it could help.

Categories

Resources