Pycharm path issues - python-3.x

Im testing the very first line of a text extraction program in Python with PyCharm. With only a couple lines of simple code, the editor is raising errors on nearly every word
Ive done some googling about path configuration and tried reading the official documentation as well. I think is has something to do with Working Directory? But I can't find a clear definition for the term and dont want to sabotage myself by ignorantly messing with path variables
import os
test = os.listdir(
/Users/.../Library/Application Support/Steam/steamapps/common/Europa Universalis IV/history/countries
)
print(test)
The errors are: UNUSED IMPORT STATEMENT and UNRESOLVED REFERENCE (on every term in the path and os)
What am I doing wrong? I imported and used a different module this afternoon with 0 issues on a separate file. Ive changed none of the interpreter or config settings between the last file and this one.

In pycharm, you need to go to File -> Settings -> Under Project(current project name) -> Project Structure and "Add Content Root" of all the directories that you import your files from.
And make sure you check the option for "add content roots to PYTHONPATH"(File-> Settings-> Python Console from the Console dropdown) as following:

Related

Python - Analyze dependency at the level of line

I am curious to know if there is an existing tool/package that can show of the dependencies line by line inside the files.
A very primitive example:
Say, we want to investigate the dependency of the following file
# main.py
import math
def sqrt_wrapper(x):
return math.sqrt(x)
I would like my analyzer to show something like the following
Dependency result:
main.py:
line: 2 -> math
line: 2 -> math
If it does not exists, I would also welcome your ideas about the possibility of such a tool.
Note: I searched for such a tool, but the ones I found were all showing only modules/files without going inside the file.
I tried to find such a package on github/reddit/stackoverflow, but no success

The file is in the same directory but cannot be imported

The python version I am using is 3.8.2
I searched a lot and most of the solutions are to use sys.path.append()
But it didn't solve the problem for me, if I use from . import players
it will say
ImportError: attempted relative import with no known parent package
if i use import players it will say
ModuleNotFoundError: No module named 'players'
The code I used to fix this:
sys.path.append(".")
sys.path.append(os.getcwd() + "\\players.py")
sys.path.append(os.getcwd())
still can't fix this,It's worth mentioning that at some point sys.path.append(os.getcwd() + "\\players.py") can run
Make sure that the name of the file is actually players.py.
And not for example players.py, players .py (note the spaces).
Also check for all other "invisible" Unicode characters that would not necessarily show up.
And make sure that there is no directory players as well.
I deleted the file completely so I couldn't test it, but I ran into this problem again, it's an import order problem, I tried to import attributes.py first and then content.py, there was a problem Check, can't find attributes.py , the problem is solved when their import order is swapped, I can't understand why, but if you encounter such problems please try once (other files are not imported)
I've had similar issues and I've created a new, experimental import library for Python to solve this kind of import error: ultraimport
Instead of from . import players it allows you to write
import ultraimport
players = ultraimport('__dir__/players.py')
This will always work, no matter how you run your code, no matter what is your current working directory and no matter if there's another directory somewhere called 'players'.

How to fix 'from caffe.proto import caffe_pb2' error in python?

i get the following error in my code in pycharm when i try to import caffe_pb2 : "Inspection info: This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items". How can i resolve this error ?
from caffe.proto import caffe_pb2
Try adding this to the Python console:
sys.path.extend([/home/user/caffe-master/python])
Plus, add the following lines to your code:
import sys
sys.path.append("/home/user/caffe-master/python/")
import caffe
This worked out for me.
Otherwise, you can go to the project interpreter and directly add it to the python PATH. Go to Settings -> Project Interpreter -> select the specific folder that you're working on -> open the bar with all the possible interpreters and press show all -> click the last of the options -> add the path (/home/user/caffe-master/python)

Importing module: Exception has occurred: NameError name 'name' is not defined

I'm new to python and i'm making a program to delete items older than 3 days in a specific folder. Now everything works fine, but i want to split up my file extensions to a module (so it's easier to add or delete extensions later) and import it into my main program.
Now i have following code:
extensions.py
extension = [".pdf",".csv",".xls",".xlsx",".txt",".DS_Store",".jpg",".png",".docx",".zip"]
Deleter.py
import extensions
for ext in extension:
print(ext)
I'm using Visual Studio Code to write my program and it underlines the "extension" in my Deleter.py file.
I've been searching for hours now and tried everything (have an empty init.py file in the same folder, tried to work with subfolder,... but nothing works). I'm using Python 3.7
Can someone help me please?
The problem here is that you are not creating any instance of extensions. So try this:
import extensions
for ext in extensions.extension:
print(ext)

How to build python class structure for matplotlib to export ot .exe with cx_freeze?

I built a code to generate and play random musical notes. It is working great within python, but I would like to make it into an .exe stand alone program, so people without python can use it. I show an image below of the output. It creates a matplotlib figure with a 'TkAgg' backend. There are 5 buttons and an user entry box that all work.
I used cx_freeze to try to package it, and I worked through all of the errors. I also got some of the examples to work. I can see the the build folder is getting the 4 Images and many .wav files I need to draw the musical staff and play the notes. One error showed that the .exe tried to run my code, because it couldn't find the .wav files). I changed how I specified where they were for the .exe. But now when I run the .exe nothing happens.
Unfortunately my code is a monstrosity. It's messy, and somewhat long (750 lines if you count white space). The .py file I am trying to write to the .exe is Interval_Trainer_v1_1.py. It can be found here.
Because it works in python, but not in the .exe, I thought it might have to do with my ignorance of how to use classes in conjunction with plotting well. Basically I call the class, and then initialize a bunch of things so I can refer to them later. That allows me to delete notes I've plotted before, old answers, etc.
How can I practice building up 'TkAgg' backended figures that will execute properly after cf_freeze? I feel like I need to start with some basic ideas and build up to my application, which is fairly complex.
One note, I do use pygame for the sounds.
Here is my setup file:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tk8.6'
import sys
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
additional_mods = ['numpy.core._methods', 'numpy.lib.format',"matplotlib.backends.backend_tkagg", 'matplotlib.pyplot', 'matplotlib.image', 'matplotlib.widgets']
setup(
name = "Interval Trainer",
version = "1.0.0",
author = "Bart",
author_email = "bcubrich#gmail.com",
options = {"build_exe": {'includes': additional_mods,"packages":["pygame","tkinter",'random'],
"include_files": [
'Images/F cleff 8vb.png', 'Images/F cleff.png',
'Images/G cleff 8vb.png', 'Images/G cleff.png',
'Pitches/A#1.wav', 'Pitches/A#2.wav', 'Pitches/A#3.wav',
'Pitches/A#4.wav', 'Pitches/A#5.wav', 'Pitches/A1.wav',
'Pitches/A2.wav', 'Pitches/A3.wav', 'Pitches/A4.wav',
'Pitches/A5.wav', 'Pitches/Ab1.wav', 'Pitches/Ab2.wav',
'Pitches/Ab3.wav', 'Pitches/Ab4.wav', 'Pitches/B#2.wav',
'Pitches/B#3.wav', 'Pitches/B#4.wav', 'Pitches/B1.wav',
'Pitches/B2.wav', 'Pitches/B3.wav', 'Pitches/B4.wav',
'Pitches/B5.wav', 'Pitches/Bb1.wav', 'Pitches/Bb2.wav',
'Pitches/Bb3.wav', 'Pitches/Bb4.wav', 'Pitches/C#2.wav',
'Pitches/C#3.wav', 'Pitches/C#4.wav', 'Pitches/C#5.wav',
'Pitches/C2.wav', 'Pitches/C3.wav', 'Pitches/C4.wav',
'Pitches/C5.wav', 'Pitches/C6.wav', 'Pitches/D#2.wav',
'Pitches/D#3.wav', 'Pitches/D#4.wav', 'Pitches/D#5.wav',
'Pitches/D2.wav', 'Pitches/D3.wav', 'Pitches/D4.wav',
'Pitches/D5.wav', 'Pitches/Db1.wav', 'Pitches/Db2.wav',
'Pitches/Db3.wav', 'Pitches/Db4.wav', 'Pitches/E#2.wav',
'Pitches/E#3.wav', 'Pitches/E#4.wav', 'Pitches/E1.wav',
'Pitches/E2.wav', 'Pitches/E3.wav', 'Pitches/E4.wav',
'Pitches/E5.wav', 'Pitches/Eb2.wav', 'Pitches/Eb3.wav',
'Pitches/Eb4.wav', 'Pitches/F#1.wav', 'Pitches/F#2.wav',
'Pitches/F#3.wav', 'Pitches/F#4.wav', 'Pitches/F#5.wav',
'Pitches/F1.wav', 'Pitches/F2.wav', 'Pitches/F3.wav',
'Pitches/F4.wav', 'Pitches/F5.wav', 'Pitches/G#1.wav',
'Pitches/G#2.wav', 'Pitches/G#3.wav', 'Pitches/G#4.wav',
'Pitches/G#5.wav', 'Pitches/G1.wav', 'Pitches/G2.wav',
'Pitches/G3.wav', 'Pitches/G4.wav', 'Pitches/G5.wav',
'Pitches/Gb1.wav', 'Pitches/Gb2.wav', 'Pitches/Gb3.wav',
'Pitches/Gb4.wav']}},
executables = [Executable("Interval_trainer_v1_1.py", base=base)],
)
Output Image
Any help is appreciate.
See the matplotlib user interfaces examples embedding_in_tk and embedding_in_tk2 to practice building up TkAgg backended figures.

Resources