I'm fairly new to python programming, i'm familiar with the very basic stuff and i'm currently learning about creating function definitions in scripts.
In particular i'm a mac user and i'm using text wrangler to write and run my python programmes.
Now that i have learned how to define basic functions in scripts i have questions which my notes do not seem to answer.
How do i import my definition which is saved in a file on my desktop to use on IDLE? I've tried
import fileaname
in IDLE and it does not work.
Secondly Suppose i create a function A in one script and then another function B in a separate script that depends on A, do i have to import A in the script for B first? Do they need to be saved in the same file?
I appreciate any advice and useful tips.
With import filename , you want to make sure its in the same directory as your original file. Try using this too
import sys
sys.path.append(directory_path)
It seems to be a common issue.
Related
I have to maintain an oll code running with pyspark.
It's using a method I've never seen.
I have some reusable code zipped into a file ingestion.zip.
Then, this file is called using a pipeline.cfg file like this:
[spark]
master=spark://master
py-files=${HOME}/lib/ingestion.zip
spark-submit=${SPARK_HOME}/bin/spark-submit
When I'm trying to import the library as shown below, I cant make Pycharm understand that the lib should point to the zip file.
from ingestion.data import csv, storage
I've seen the zip is a solution proposed by spark-submit using py-files but how can I make it running on my IDE ?
I haven't used below method with pycharm, but it worked for us with spark-submit and we could import these modules using normal import statements.
Actually, we had a very few files to import and we needed something quick. So, if you also have the same use-case and if pycharm allows then maybe, you can give it a try.
--py-files s3://bucket-name/module1.py,s3://bucket-name/module2.py,s3://bucket-name/module3.py,s3://bucket-name/module4.py"
(Note - there shouldn't be any spaces.)
(Note - this suggestion is only an interim solution till someone replies with a better answer.)
I am doing an online Python course and currently learning about modules and packages, but can't seem to get my head around the logic. I have used modules (or maybe they were packages who knows?) before in current job but without really knowing what was happening when I would do import Pandas for example.
By following the course I have created a folder called "MyMainPackage" and in this folder there is another folder called MySubPackage.
In "MyMainPackage":
init.py and my_main_script.py; in my_main_script.py there is a function called main_func()
in "MySubPackage":
init.py and my_sub_script.py; in my_sub_script.py there is a function called sub_func()
In Sublime Text editor I wrote a script called myprogram2.py:
import MyMainPackage
MyMainPackage.my_main_script.main_func()
However, this hasn't worked; when I call python myprogram2.py in Windows Command Prompt I get the following message
Attribute Error: module MyMainPackage has no attribute my_main_script
However, as per the online lecture, what does work is this:
from MyMainPackage import my_main_script
from MyMainPackage.MySubPackage import my_sub_script
my_main_script.main_func()
my_sub_script.sub_func()
Why can't I just import the entire package and access the modules like I have tried to above instead of the way the online lecture does it? I thought it would be the same thing. I am just struggling to understand the logic.
I have a Python script that extracts certain consumer product aspects from customer reviews using LinearSVC, but I am trying to convert this script into some sort of API to use for new reviews. Is there an easy way to do this? I am very new to the whole concept of APIs.
An API is just a library you import once it's reachable by the interpreter in your case. So any import in python is you calling on an library/API.
So if you're script is called foobar.py for example, if it is in the same directory as other python files using
import foobar
at the top of your python file should allow you to reference any functions made in your original python script.
I really tried to find an answer to my problem but couldn't find one, so I hope I didn't double post.
I am running a raspberry pi using debian and python27. After boot up I run a script which determines some general variables I will need for other scripts. I need this variables in different scripts because they are running during different times using crontab.
Normally after the boot up the script is done and I have all my necessary variables in a text file.
I can access from all different other scripts to this text file.
But know I try to avoid to corrupt my SD card of my raspberry and I want to use the read-only mode.
Is there an opportunity to use variables which are created/changed by a python script without using a text file?
I could also run a script with an infinite loop, but I try to avoid this.
Any other suggestions? Is it possible to use environmental variables?
Thanks for every answer!
Max
Edit1:
Thanks for the suggestion the answer from adrianX, but I tried this small example and it doesn't work:
script1:
import os
os.environ["variable1"] = "value1"
After executing the script1, I run the second script.
script2:
import os
print os.environ.get["variable1"]
But this doesn't work? Maybe my question wasn't clear enough?
A non python specific solution would be: http://www.domoticz.com/wiki/Setting_up_a_RAM_drive_on_Raspberry_Pi and writing the file to the /var/tmp the file will disappear on reboot of course.
I'm a java developer new to python. In java, you can access all classes in the same directory without having to import them.
I am trying to achieve the same behavior in python. Is this possible?
I've tried various solutions, for example by importing everything in a file which I import everywhere. That works, but I have to type myClass = rootFolder.folder2.folder3.MyClass() each time I want to access a foreign class.
Could you show me an example for how a python architecture over several directories works? Do you really have to import all the classes you need in each file?
Imagine that I'm writing a web framework. Will the users of the framework have to import everything they need in their files?
Put everything into a folder (doesn't matter the name), and make sure that that folder has a file named __init__.py (the file can be empty).
Then you can add the following line to the top of your code:
from myfolder import *
That should give you access to everything defined in that folder without needing to give the prefix each time.
You can also have multiple depths of folders like this:
from folder1.folder2 import *
Let me know if this is what you were looking for.