I like to organize my data analysis projects with a root folder containing a data folder (named data) and a code folder (when I use R, I sometimes name it R). When using Python, my code folder would logically be called python. My project working directory is the root folder (containing both data and python folders).
I have multiple files of code in my python folder. I would like to import one (let's call it file2.py) from another, in an interactive python session. I have tried the following:
from .python import file2
However, this does not work because python is a reserved word. If I rename the folder to something different, it works. Is there a way to escape the reserved word, or pass the file location as a string to the import? I'm frustrated because all the best alternatives (e.g., code, scripts) are also reserved words. My project organization is an oft-recommended best practice in R, and I'm hoping there's a parallel in Python.
Your code is not running as a package. Therefore, you need to run from python import file2, not from .python import file2.
Related
I have been using manim and manim_editor to craft some presentations for my phD (see my github https://github.com/PanoPepino and the deployments)
I would like to know how to optimise the structure of my projects, so I can have a better file organisation. What I would like to do is the following:
manim is a set of libraries that I installed making use of pip3 in the terminal. On the other hand, manim_editor requires me to specify the global path in my main.py file.
The first picture correspond to my current directory. Inside Manim_Material I have some modules (?) with all functions I have crafted over the last year. Inside Presentation_ABC folders, I have two .py files: Text.py, that contains the text I would display for each presentation and the Main_XX.py file, that contains the commands to organise each of the animations in the output file that generates the presentation.
This Main_XX.py file contains the commands you can see in the second picture.
When I try running "manim --save_sections Main_XX.py -pql" in the terminal, I get the following message (3rd Picture)
The question is: What files and/or line commands should I add to my code (in any of the files that main.py file calls) such that I can run manim with this folders configuration? I do not really understand why if I set the main.py file one level above Manim_Material, it works fine.
Thank you for your help in advance,
Panizo.
Folder set up
Code in the manim file maim
Error when running manim
I tried to used the init files inside each of both folders (Manim_Material and Presentation_ABC) but got same error.
I tried to compile main.py one level above Manim_Material (outside Presentation_ABC) and it runs without a problem.
I would like to transform Manim_Material in some sort of global library.
I have a python package folder, I am using regularly. As part of my work, I have multiple versions of this package folder.
The folder look something like this,
Main folder
scripts.ipynb
other stuff
package folder
init.py
all submodules folders
The init.py file consists of 'import package_name.submodule_name' lines. I have many main folders, like this one, for all the different versions. If I run the scripts.ipynb notebook, then it works correctly with the wanted package version.
But if I run the notebook, outside of the main_folder, it seems I can not import the package correctly. I want to have the ability to run single scripts notebook, where I can toggle and switch between the different main folders/packages.
Giving unique name for every package is very tedious work, because the name of the package is repeated numerous time along the files. I tried to append the python path to the system, or set the PYTHONPATH to look at the correct folder, but it doesn't help.
In the case of appending the python path to sys, the import routine goes to the correct init.py file, but inside the file it fails to import the submodules. When setting the PYTHONPATH to the folder, no error is given, but the functionality is incorrect (error at first call).
I am working on a python project that depends on some other files. It all works fine while testing. However, I want the program to run on start up. The working directory for programs that run on start up seems to be C:Windows\system32. When installing a program, it usually asks where to install it and no matter where you put it, if it runs on start up, it knows where its files are located. How do they achieve that? Also, how to achieve the same thing in python?
First of all, what do you mean by "their files"? Windows applications can store "their files" in multiple places (including but not limited to %CommonProgramFiles%, %ProgramData% and %AppData%).
That being said, the common location for simple applications and scripts is to use the same directory as the .exe (or script).
In Python there seems to be multiple ways to find this path, this seems to work nicely:
import os
print(os.path.abspath(os.path.dirname(__file__)))
See also:
How do I get the path of the Python script I am running in?
How do I get the path and name of the file that is currently executing?
If you plan to consume local files that contain raw data or processed data, defining a default directory or a set of directories can simplify your implementation, for example:
Place your data files under a specific set of folders in C:\ or place your files under the F:\ folder, that can be a part of your on premisses file system
Based on where your Python application is located, you'll need to use relative paths or a library to help you to locate these files.
Here are some examples:
os.path
pathlib
In Python, I use some of my functions so often that I made a file where they are written, no matter the project I am working on. How do I tell Python to add this file to path every time my code is executed so that i can use my functions, just like adding a specific path to Pathtool in Matlab? I know in Python, I could do something like
sys.path.insert(1, 'D:/Python/FunctionDirectory') and then from file_with_functions import my_function but I would have to write this code to all of my projects before start coding to use my functions. I would like to always have the path to my functions added so that I can always use them like in Matlab without worrying about adding them in every single .py file.
you can use PYTHONPATH env var to specify folder where to look for modules
https://www.tutorialspoint.com/What-is-PYTHONPATH-environment-variable-in-Python
To provide complete steps for anyone with my problem for later times, I found my file with functions can be thought of as a module from which functions can be imported just like from file_with_functions import my_function.
But, I needed to add the folder with my module to python path. Messing with environment variables in Windows didn't work for me. Fortunately here, I read about a different solution:
I simply went to my site-packages folder (to find it, import sys then print(sys.path) and look for a name containing the string 'site-packages'). In this folder, I created a new text file and simply pasted the path with my module there:
like this, closed the text file and changed the extension from .txt to .pth (name of file did not matter as long as it was a .pth, Python found it).
So lets say I have a python code 'C:\Users\apps\server.py'. The code requires multiple xml, dll, config files to run. One thing I can do is copy all the files in same directory 'C:\Users\apps' and be done with it.
However, I want the code to retrieve all these files from a child directory 'C:\Users\apps\loadFiles\'.
I have tried the following code:
import sys
sys.path.append('path')
The above code helps in uploading all the DLLs but not the xmls and configs.
How can we solve this problem using __init__.py method ?
Just add your path like that
sys.path.append(r 'C:\Users\apps\loadFiles\') If that's enough. You can place that in __init__.py or in server.py as well.
The r before the string will ensure that all the backslashes are not converted.