Skip to content
Advertisement

Tag: import

How do I call a function from another .py file? [duplicate]

This question already has answers here: How do I import other Python files? (23 answers) Closed 6 months ago. file.py contains a function named function. How do I import it? The above gives an error: ImportError: No module named ‘file.py’; file is not a package Answer First, import function from file.py: Later, call the function using: Note that file is

Relative imports for the billionth time

I’ve been here: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python packages: relative imports python relative import example code does not work Relative imports in python 2.5 Relative imports in Python Python: Disabling relative import and plenty of URLs that I did not copy, some on SO, some on other sites, back when I thought I’d have the solution quickly. The forever-recurring question is this:

Can’t import my own modules in Python

I’m having a hard time understanding how module importing works in Python (I’ve never done it in any other language before either). Let’s say I have: Now I’m trying to get something like this: However, I’m definitely doing something wrong as Python can’t see that myapp is a module: Answer In your particular case it looks like you’re trying to

Importing modules inside python class

I’m currently writing a class that needs os, stat and some others. What’s the best way to import these modules in my class? I’m thinking about when others will use it, I want the ‘dependency’ modules to be already imported when the class is instantiated. Now I’m importing them in my methods, but maybe there’s a better solution. Answer If

Python import src modules when running tests

My source files are located under src and my test files are located under tests. When I want to run a test file, say python myTest.py, I get an import error: “No module named ASourceModule.py”. How do I import all the modules from source needed to run my tests? Answer You need to add that directory to the path: Maybe

Check if module exists, if not install it

I want to check if a module exists, if it doesn’t I want to install it. How should I do this? So far I have this code which correctly prints f if the module doesn’t exist. Answer Here is how it should be done, and if I am wrong, please correct me. However, Noufal seems to confirm it in another

Can’t import Numpy in Python

I’m trying to write some code that uses Numpy. However, I can’t import it: I tried the suggestions in this question: and I searched for files named numpy in that path: But nothing came up. So… Are there any other places in which Python modules are commonly installed? How can I install numpy locally in my account, if it turns

Advertisement