This is the first time I have attempted to use anything other than what's provided by python.
I have recently gotten into pythons provided Tkinter, though due to some issues I decided to use another GUI, and heard that PyQt was highly recommended, so I downloaded that and looked into various tutorials. In these tutorials, I cannot seem to execute any of the import statements in said tutorials that relate to PyQt, primarily PyQt5 (I have checked I have the correct version number by the way).
So for instance:
import PyQt5
raises the error:
Traceback (most recent call last):
File "/Users/MEBO/PycharmProjects/Music/testing.py", line 1, in <module>
import Qt
ImportError: No module named 'Qt'
[Finished in 0.1s with exit code 1]
I have a lot of research into this. I've heard people talk of using pip to install modules, and I have done this be safe (as well as downloading it from the internet), I've tried changing the project interpreter to versions Python3/ 2.7/ 2.6, appending the path name to the sys.path directory, (which I really know nothing about to be honest, I was hoping I'd get lucky), though nothing seems to work.
Are you supposed to be able to just import a module off the bat, or do you have to set some things up first?
For windows download the package and extract it to (path where python installed)\Python27\Lib and then try to import.
Specific to PyQt
This package cannot just be downloaded and imported, it must be built because it is not pure python, it uses Qt (C++) and requires dependancies. Read this tutorial on installation.
There is also a very complete python package distribution, Anaconda, that includes pyqt and much more. Almost all the packages I ever looked at are in there.
In general to pure python code
In other cases, if you place modules/code that has been download into the directory that your python script is run from, you can import off the bat, or you can append/insert any folder to the sys.path.
# importer will search here last
sys.path.append('/path/to/code/')
# importer will search here second, right after script's directory
# this can be useful to override a module temporarily...
sys.path.insert(1,'/path/to/code/')
Related
I'm trying to set up a roguelike Python project, but I can't seem to be able to import libtcod module into my project. This helloworld crashes, and the IDE keeps telling me that there is no module named libtcodpy.
import libtcodpy
def main():
print('Hello World!')
if __name__ == '__main__':
main()
What is the proper way to import modules into Python projects? I'm used to Java, so I was expecting something along the lines of Maven to manage the dependencies. There indeed seems to be something like that in PyCharm as well, this package manager for the venv, which from what I gather serves to isolate the project-specific stuff from the OS- or python-global stuff:
but libtcod simply isn't present in the rather exhaustive list of modules that appears after clicking on the "+" button, just some other module that has something to do with libtcod library (I guess?). Moreover, all the tutorials I found on setting libtcod up advise one to manually copy over files somewhere or run some command that I suppose does the importing somehow and other such solutions, all of which i tried and none of which worked. I don't want to pollute my project structure by using such hodgepodge ways of handling dependencies if I can at all avoid it.
Q: How do I get libtcod to work in my PyCharm project in the most clean and convention-abiding way possible?
Take a look at this github project called tcod: https://github.com/libtcod/python-tcod/blob/master/README.rst#installation
It's a python port of libtcod.
To install using pip, use the following command:
python -m pip install tcod
If you get the error "ImportError: DLL load failed: The specified module could not be found." when trying to import tcod/tdl then you may need the latest Microsoft Visual C runtime.
Blockquote
I'm working through Learn Python the Hard way, and I'm currently on exercise 46 where you learn to create packages. I've created a basic package that does a few calculations, and uses a few different modules.
I've gotten the package to install in my python2.7 site packages, but I can't seem to run the module from my site packages after the fact. I'm wondering if the path that python is searching is different, because of the following:
After the install, I see this message Copying story-0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages
However, when I try to run the module, I see this message /usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'story.py': [Errno 2] No such file or directory
Sorry if this makes absolutely no sense, I'm very new to the fantastical world of programing.
When you install a package, you access the internals differently. You can no longer just call python story.py.
To access the functions in story.py you need to import the module at the top of another python file, or in the interpreter.
If story.py contained
def my_test_function(blah):
print blah
You would use this function in another file by the following (after installing the module, as you've already done)
import story
story.my_test_function("Hello!")
By importing the module, you get access to all the functions and classes inside it by typing module_name.function_name. You could also just import the function you wanted to use directly, which would look something like
from story import my_test_function
my_test_function("Hello!")
There is a bunch of pre-made code I simply have to run. However, the code requires the module NumPy, which for some reason is not installed. I am using Geany for the Python code, and when I click compile, the console states:
"Traceback (most recent call last):
File "chi2Fit.py", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
I really tried to look it up, and I found the zipped NumPy packages on SourceForge, but I cannot install them. I am using Windows 10 (Virtual Machine is buggy, unfortunately). I usually run my Python programs by compiling them directly and don't use the commands within the console (so honestly I don't know how to open the command line. Yes, I am a beginner). What is the explanation?
Geany is just an editor, with the ability to run commands and such from menus and buttons. So you should be able to run this code 'by compiling them directly'. If that works while the 'geany compile' does not, then you need to check the 'compile' command. Is it using the right Python?
'compile' really doesn't make sense with Python code. You are just executing a script.
NumPy is a large package that does include compiled components. So installing it requires more than downloading a ZIP file. On Windows it is usually best to install one of the precompiled packages. Anaconda is popular one.
But we know nothing about this pre-made code, and whether it has included the necessary modules or not. You need to find out from the source of that code what is needed to run it.
You need to figure out how to install NumPy. It depends on what OS you are using.
There are pre-compiled packages for Windows. There is plenty of information here on SO (e.g., Installing NumPy on Windows) or you can use Google.
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
I'm trying to run a Python program, which uses the pygame modules, from MATLAB. I know I can use either
system('python program.py')
or just
! python program.py
However, I keep getting the error:
Traceback (most recent call last):
File "program.py", line 1, in <module>
import pygame
ImportError: No module named pygame
What is strange is that if I run the program from the command line, it works just fine. Does anyone know why if run from within MATLAB, Python can't find pygame?
The problem may be that MATLAB is not seeing your PYTHONPATH, which normally stores Python libraries and modules. For custom modules, PYTHONPATH should also include the path to your custom folders.
You can try setting the value of PYTHONPATH from within a MATLAB running session:
PATH_PYTHON = '<python_lib_folder>'
setenv('PYTHONPATH', PATH_PYTHON); % set env path (PYTHONPATH) for this session
system('python program.py');
See also the possibly relevant SO answer here: How can I call a Qtproject from matlab?
As I haven't used matlab too often and don't have the program available now I cannot say for sure, but matlab may be creating a custom environment with custom paths (this happens a lot, so the user has a very consistent experience in their software). When matlab installs it may not export paths to its own modules to your default environment. So when calling for pygame.py outside of matlab, python cannot find pygame.py under its usual lookup paths.
Solutions could be:
find the pygame.py, and map the path to it directly in your code, though this could cause you headaches later on during deployment
Try just copying the pygame.py file to your working directory, could have dependences that need to addressed.
Install pygame directly from its developer at http://www.pygame.org. Version differences could be a problem but pygame gets put under the usual lookup paths for python. (This would be my preferred solution personally.)
Or just export the location of path to pygame in matlab's library to your default enivronment. This could be a problem during deployment too.
For posterity, first try everything that Stewie noted here ("Undefined variable "py" or class" when trying to load Python from MATLAB R2014b?). If it doesnt work then it's possible that you have multiple pythons. You can try and check which python works (with all the related installed modules) on your bash/terminal. And then use
pyversion PYTHONPATH
to let matlab know the right path.
Also use py.importlib.import_module('yourmodule') to import the module after that.
That should get you started.