how to import files into python - python

I'm working on Learn python the hard way and am not a very experienced programmer and I have searched on Stackoverflow and other programming websites but I have not found the answer to my question. Well anyway my question is how do you import files into another file in python what I have seen is that many people have answered this by saying that you should just put import filename but when I do this I get the error Traceback (most recent call last):
File "source.py", line 1, in <module>
import one
ImportError: No module named filename so how do I import a file without getting this error. if anybody would like to know where to go to see why I'm asking question go to http://learnpythonthehardway.org/book/ex45.html. I'm also using a chromebook so I have an app named source lair instead of the traditional python software, so if anybody has a chromebook or chromeos computer please give me suggestions as to if this is a good app and if there's a better app. ps: you can download the app from the chrome store on any operating system.
print "thanks"

just put the file "filename.py" into the same dir as source.py if you sure you have created a file named "filename.py". Then import filename in source.py will work.

python looks for modules in sys.path.
in python
import sys
print sys.path ... it will print the list of directories where it look for modules
you can add a directory in sys.path
sys.append("name_of_directory")
or you can set PYTHONPATH environment variable for directory.

Related

PyScripter: Why might some modules import personal modules while others dnot?

I am using PyScripter to implement a lesson from the Python Tutorial: https://docs.python.org/3.7/tutorial/modules.html#more-on-modules
However, when I attempt to import my own module quadform.py in a separate code by calling
import quadform
quadform.quad_form(1,1,-6)
I get the error:
Traceback (most recent call last):
File "<module1>", line 1, in <module>
ModuleNotFoundError: No module named 'quadform'
Even more confusing is that when I copy + paste the exact code into a separate module saved in the same location, the import on the second module does works. Since then, I have tried various permutations of syntax, using different IDEs, calling from different modules, calling from the shell, and fiddling with the Path in Environment Variables with only a few successes and seemingly at random.
Can someone explain what might be the issue? At the moment, I cannot reliably call any of my modules without getting the same error. I am a beginner trying to pick up Python (and programming in general) on my own so any help would be greatly appreciated.
Read further down:
https://docs.python.org/3.7/tutorial/modules.html#the-module-search-path
https://docs.python.org/3.7/tutorial/modules.html#packages
https://docs.python.org/3.7/tutorial/modules.html#intra-package-references
If you structure your modules like it says, you should be able to import the right things. sys.path is a list of the folders Python looks in to find packages.

ImportError: Unable to find an implementation of PyGUI for this installation

I had a simple app up and running that used the PyGui package. After I decided I had everything working I refactored my code and basically broke it up over multiple files, but I am fairly sure I did not change the location of any files. Now when I try to run my app It gives me the following error:
Traceback (most recent call last):
File "blobedit.py", line 16, in <module>
from GUI import Application, ScrollableView, Document, Window, Cursor, rgb
File "/Users/<myusername>/Desktop/BlobEdit/GUI/__init__.py", line 54, in <module>
raise ImportError("Unable to find an implementation of PyGUI for this installation")
ImportError: Unable to find an implementation of PyGUI for this installation
I have tried moving files around for weeks and can't find the problem. Any help would be much appreciated!
You have a directory GUI and inside it __init__.py, this makes the directory an importable package.
Unfortunately, PyGUI also provides a GUI package. Due to the way Python searches for packages to import, your GUI has come up first and this is what is causing problems.
The simple way to solve it is to rename your GUI directory /Users/Destkop/BlobEdit/GUI to something else.

Can only import win32com.client with IDLE. What extra work do i need to set up pywin32?

I have a Tkinter program that i use for time management. I decided to have it scan my inbox in outlook to check emails for tags, and then based on the tag, add it to my list of tasks to do for the night.
The code i wrote works just fine, but i'm getting an error when I import win32com.client. I can import it in IDLE, but it is having problems importing when i try to run the code with a .bat file, or double clicking the .py file.
I have found several people with similar problems, but from what i can tell, it's having problems with win32api module, or pywin32
Traceback (most recent call last):
File "my_program_filename.py", line 1, in <module>
import win32com.client
File "c:/Python27/lib/site-packages/win32com/__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found
I'm really confused. When i get the sys.path it's the same with IDLE as it is running from the .py file with the exception of an added "c:/users/username/desktop/timer" for my .py file.
I'm really lost and haven't had to mess with the system path, but I'm not able to figure out what i need to do to fix this.
What do I need to do to get pywin32 working so I can use win32com.client?
Any help is greatly appreciated. Thanks!
IIRC, the problem was attempting to link to the debug build. I think i had to dupe the release build and rename it to be debug or something.
try building release and see if it "just works". if so, you have a direction to explore
this is an issue of not having the correct paths in the sys.path. If you make sure that all of the pywin32 folders are in the sys.path(If you check in IDLE it can show that the folders are included even when they aren't?!?!?).
You also have to make sure you run your code from inside your Python directory or it will fail to import win32api. I also found that if you do anything in a function that uses pywin32 and you accidentally misspell the function when you call it, the whole import fails without telling you your function is misspelled. You can also navigate to the /Python27/Lib/site-packages/win32com/client folder and run makepy.py to make sure the right Object library is installed.
When you run makepy.py, you select the COM object you want to use and it creates packages specific to what you want to use. I don't fully understand why this is, but once i did this and ran my file from the Python folder it worked! There is a more in depth explanation on how to get this working right here.
I found this link to be the solution rather than the win32com/client as indicated above: win32com import error python 3.4
in my case typing in cmd:
python C:\Python27\Scripts\pywin32_postinstall.py -install
I hope this helps

managing python import dependencies in SublimeText2 plugin. specifically unicodedata lib

I'm trying to fix a little SublimeText2 plugin. the problem seems to be (from SublimeText python console) to be an import error:
Traceback (most recent call last):
File ".\sublime_plugin.py", line 62, in reload_plugin
File ".\rtl.py", line 4, in <module>
from algorithm import get_display
File "lang\algorithm.py", line 20, in <module>
from unicodedata import bidirectional, mirrored
ImportError: No module named unicodedata
since unicodedata is the standard python library, I tried to import it directly in the console and got the same import error. I figured this is because sublimetext isn't using the system installed python version (I'm using python2.7 in a windows machine) but It's own bundled 2.6 python which doesn't bundle the whole standard library.
when I try the import from my usual python interpreter everything works fine.
I tried adding a .pth file that points to "c:\python27\lib" and site-packages etc. which didn't help. adding directly to the path like this:
sys.path.append(c:\\Python27\\lib)
didn't help either. also tried to tweaked my user setting file to include:
{
"PATH": "C:\\Python27;c:\\Python27\\Scripts",
"PYTHONPATH": "C:\\Python27\\Lib;C:\\Python27\\Lib\\site-packages;C:\\Python27\\DLLs"
}
my question divides into two:
how to solve this problem specifically on my dev computer
what is the right way to solve this universally for people trying to install the plugin. making the plugin dynamically aware of the default python installation and adding it to the path.
Also, the strangest thing: The main SublimeText folder in program files actually has the unicodedata.pyd file in it. so I can't figure what the problem is!
Looks like this might be a ST2 bug rather than something you're doing wrong:
http://www.sublimetext.com/forum/viewtopic.php?f=3&t=3462

running pronterface in python: import error: no module named

I am a complete beginner to programming, python, pronterface and 3d printing so unsurprisingly I am having a few difficulties.
I have now managed to download pronterface and get it running on my computer, i think i followed the download instructions correctly and have skeinforge downloaded also in the printrun folder.
My problem is that when I try to open or load a .stl file I get the following:
Skeining C:\Users\James\Downloads\70mm_Faberdashery_Yoda-Lite_less_robe.stl
Skeinforge execution failed.
Traceback (most recent call last):
File "C:\Users\James\Desktop\3d printer software\pronterface\Printrun-d482c66\pronterface.py", line 940, in skein_func
from skeinforge.skeinforge_application.skeinforge_utilities import skeinforge_craft
ImportError: No module named skeinforge_application.skeinforge_utilities
However the file is in the right place I think:
C:\Users\James\Desktop\3dp\pronterface\Printrun-d482c66\skeinforge\skeinforge application\skeinforge utilities\skeinforge_craft
Can you try to explain in plain language any ideas to fix this as I am a total beginner and don't even really know what a module import error is.
Someone on the reprap forms was having a similar issue, the recommendation there is to run skeinforge from the command line.
To solve the problem more satisfactorily, you'd need to check what path is available to python and make sure that the path to the skeinforge module is in your system path or the python path.

Categories

Resources