ImportError: No module named... (basics?) [closed]

2024/9/20 12:26:59

A very basic question that has been confusing me. Can't seem to find any solutions online so far...

I have a simple script and want to import another script I just made in the same directory.

What is the proper way of doing this?

I just tried combos of import myfile, from myfolder import myfile, import myfolder.myfile, etc

I get ImportError: No module named 'myfile'

Cheers

Answer

This is because your current directory isn't on the PYTHON_PATH, which is what import searches when you call it. See the documentation.

If you want an immediate fix, you can use the following:

import sys, os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

This adds the directory containing the script's file to the python path.

This will only work if the directory these scripts are in is set up as a package.

https://en.xdnf.cn/q/119218.html

Related Q&A

How to pass python list address

I want to convert c++ code to python. I have created a python module using SWIG to access c++ classes.Now I want to pass the following c++ code to PythonC++#define LEVEL 3double thre[LEVEL] = { 1.0l, 1…

Python open says file doesnt exist when it does

I am trying to work out why my Python open call says a file doesnt exist when it does. If I enter the exact same file url in a browser the photo appears.The error message I get is:No such file or direc…

How to map one list and dictionary in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Sorting date inside of files (Python)

I have a txt file with names and dates like this name0 - 05/09/2020 name1 - 14/10/2020 name2 - 02/11/2020 How can I sort the text file by date? so that the file will end up like this name2 - 02/11/202…

Dicing in python

Code:-df = pd.DataFrame({col1:t, col2:wordList}) df.columns=[DNT,tweets] df.DNT = pd.to_datetime(df.DNT, errors=coerce) check=df[ (df.DNT < 09:20:00) & (df.DNT > 09:00:00) ]Dont know why this…

How to collect all specified hrefs?

In this test model I can collect the href value for the first (tr, class_=rowLive), Ive tried to create a loop to collect all the others href but it always gives IndentationError: expected an indented …

How do I sort data highest to lowest in Python from a text file?

I have tried multiple methods in doing this but none of them seem to work The answer comes up alphabetically insteadf=open("class2.txt", "r") scores=myfile.readlines() print(sorted(…

Determine if a variable is an instance of any class

How to determine if a variable is an instance in Python 3? I consider something to be an instance if it has __dict__ attribute.Example:is_instance_of_any_class(5) # should return False is_instance_of…

Method POST not allowed with Django Rest Framework

Im trying to create a JSON API compliant rest service using Django Rest Framework JSON API: https://django-rest-framework-json-api.readthedocs.io/en/stable/index.htmlI think Im stuck at the Django Rest…

Running multiple queries in mongo`

I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesnt let you use joins so I would need to run separate queries and join the results into a si…