I have a file with a function. I want to import the file and when doing that I get No module named ex25.
import ex25
I have checked some tutorials and the offial documentation.
import ex25
No module named ex25
First, check if the file is .py file and in the same folder with the file that you are currently working on, there shouldn't be any problem at all.
If not in the same folder, but in a different folder and make sure that your file is .py file, you need to make that folder (which contains the file) to a package by adding a file name __init__.py
Related
I have the following folder structure and a user created file name paths.py that has functions to deal with path related operations
project_home:
-paths.py
project_1:
main.py
project_2:
main.py
etc
when I try to import paths in main.py in the subfolders it say, it works only if I call the file from the project home only ...
error :
import paths
ModuleNotFoundError: No module named 'paths'
any idea on what I could do to import files from home directory ... usually if the files are under subfolders we can call them using "Import subfolder.file" since its in main working directory the command Import paths is failing is what I am feeling ...
I have a directory structure like this:
project/
nb/
notebook.ipynb
python/
data/file.p
settings.py
main.py
I am loading file.p under settings.py by writing open("data/file.p","rb").
Then I would simply write from settings import * in main.py and it loads the file. But when I write the same in notebook.ipynb it does not load the file because the current directory changes to nb/. Is it possible to load this file in notebook.ipynb without changing the directory path inside settings.py ?
Python is a package and settings is a module. So try this.
In notebook.ipynb
from python import settings
If it is not able to recognize the path, then set PYTHONPATH to absolute path of project directory and run again.
I have downloaded a csv file(ABC) using python and selenium and need to change the name of the file.
Input= ABC.csv #downloaded file with name ABC
Output= DEF.csv #New file with name DEF.
Any help is appreciated.
try this
import os
os.rename('a.txt', 'b.kml')
From:
How to rename a file using Python
Thanks All.. Used the below code and it is working fine. All others kept throwing the error as File not found. Used complete file path.
os.rename('C:\\Users\\pathname\\ABC.csv', 'C:\\Users\\pathname\\DEF.csv')
You can use os module to rename the file:
import os
os.rename('path to ABC.csv', 'path to DEF.csv')
Explanation:
rename first argument is the original file which needs to be renamed and second argument is the new name which it should be renamed. If the files are in current directory it will work by simply using the file names. Otherwise include the path where these files are there
More information at docs **https://docs.python.org/3/library/os.html#os.rename
Professional Python newbie here. I have created a Python module called aviation with a file in there called database.py.
I have another module called core, with a file in there called calculator.py.
I want to import aviation.database.py into my calculator.py.
The basic structure is as follows:
My project
aviation (module)
- database.py
core (module)
- calculator.py
test.py
My calculator.py file has an import such as:
from aviation import database as aviation_database
This module is not recognised and I get a red squiggly line indicating as much.
If I create another file test.py outside of aviation and core and add the above import, there are no issues in this tests.py file - the import works fine.
It appears that I need to do something so that my module can import from another module... it does allow me to import installed modules (like date), but I have no idea what I am missing.
I am using the IntelliJ IDE and my code is located in the regular C:\Users\\IdeaProjects directory.
Can someone tell me what I should do and why I am facing this problem?
Well I would recommend you to create __init__ file in the modules.
The python3 doc states that :
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.
So your Directory Structure becomes
My project
aviation (module)
- database.py
- __init__.py
core (module)
- calculator.py
- __init__.py
test.py
Now import all the thing in your __init__ file that you want to us from other packages. Like in aviation's __init__.py file write
import database
Similarly, for core's __init__.py
Now in calculator.py you can import them by -
from aviation import database
Let me know, if this help !
Thanks
Its as simple as this.
For instance, you have an already written code e.g myname.py and you want to import the entire module into new code e.g newwork.py
step 1. open your code newwork.py
step 2. just as you do other import, just write:
import myname
myname
Thats all.
Now, you have your entire nyname code inside newwork. When you run newwork, it runs nymame alongside.
I hope this help somebody?
This is my directory structure
-directory1
|-currentlyWritingCode
-directory2
|-__init__.py
|-helper.py
|-view.ui
The helper.py is a UI from pyqt4 framework. It needs the view.ui file .
it has following code to load the ui data
viewBase, viewForm = uic.loadUiType("view.ui")
now in directory1 in the currently writing code I did this
import directory2.helper
when I run the code in currentlyWritingCode it is throwing an error that
FileNotFoundError: [Errno 2] No such file or directory: 'view.ui'
What to do now?
using python3.5 from anaconda on Centos 7
Use os.path.join(os.path.dirname(os.path.realpath(__file__)),'view.ui') in place of view.ui. This will ensure you correctly reference the folder that the python file lives in, regardless of the code that imports it.
Note: Make sure you have import os with your other imports.
How does this work?
__file__ is an attribute of the module you are importing. It contains the path to the module file. See this answer. However, this path is not necessarily an absolute path. os.path.realpath returns the absolute path (it even follows symlinks if there are any). At this point we have a full path to the module, so we take the path to the directory (os.path.dirname) and join it with the original filename (which we assumed to be relative to the original module and so should be in the aforementioned directory). os.path.join ensures that the correct \ or / is used when constructing a file path so that the code works on any platform.